Extending codecvt by derivation

The facet codecvt can still be customized if you are on a platform that does not support a file system, or if you do not wish to use data files for other reasons. Naturally, you can derive from codecvt and override each of the virtual methods in a portable manner as specified by the C++ standard. Additionally you can take advantage of the EWL C++ specific classes used to implement codecvt_byname. There are five implementation specific facets that you can use in place of codecvt or codecvt_byname to get the behavior of one of the five encodings:

These classes are templated simply on the internal character type (and should be instantiated with wchar_t). The external character type is implicitly char, and the state type is implicitly mbstate_t.

Note in An example use of __utf_8 is: that this locale (and wofstream) will have all of the facets of the current global locale except that its codecvt<wchar_t, char, mbstate_t> will use the UTF-8 encoding scheme. Thus the binary contents of the file is (in hex):

Listing: An example use of __utf_8 is:
#include <locale>
#include <fstream>

int main()

{

   std::locale loc(std::locale(), new std::__utf_8<wchar_t>);

   std::wofstream out;

   out.imbue(loc);

   out.open("test.dat");

   out << L"This is a test \x00DF";

}

Result

  54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 C3 9F