The following code works as expected. The source code, file \"file.txt\" and \"out.txt\" are all encoded with utf8. But it does not work when I change wchar_t
t
The various stream classes need a set of definitions to be operational. The standard library requires the relevant definitions and objects only for char
and wchar_t
but not for char16_t
or char32_t
. Off the top of my head the following is needed to use std::basic_ifstream<cT>
or std::basic_ofstream<cT>
:
std::char_traits<cT>
to specify how the character type behaves. I think this template is specialized for char16_t
and char32_t
.std::locale
needs to contain an instance of the std::num_put<cT>
facet to format numeric types. This facet can just be instantiated and a new std::locale
containing it can be created but the standard doesn't mandate that it is present in a std::locale
object.std::locale
needs to contain an instance of the facet std::num_get<cT>
to read numeric types. Again, this facet can be instantiated but isn't required to be present by default.std::numpunct<cT>
needs to be specialized and put into the used std::locale
to deal with decimal points, thousand separators, and textual boolean values. Even if it isn't really used it will be referenced from the numeric formatting and parsing functions. There is no ready specialization for char16_t
or char32_t
.std::ctype<cT>
needs to be specialized and put into the used facet to support widening, narrowing, and classification of the character type. There is no ready specialization for char16_t
or char32_t
.
std::codecvt<cT, char, std::mbstate_t>
needs to be specialized and put into the used std::locale
to convert between external byte sequences and internal "character" sequences. There is no ready specialization for char16_t
or char32_t
.Most of the facets are reasonably easy to do: they just need to forward a simple conversion or do table look-ups. However, the std::codecvt
facet tends to be rather tricky, especially because std::mbstate_t
is an opaque type from the point of view of the standard C++ library.
All of that can be done. It is a while since I last did a proof of concept implementation for a character type. It took me about a day worth of work. Of course, I knew what I need to do when I embarked on the work having implemented the locales and IOStreams library before. To add a reasonable amount of tests rather than merely having a simple demo would probably take me a week or so (assuming I can actually concentrate on this work).