c++g++localecodecvt

Codecvt doesn't work in gcc


I'm trying to use my own codecvt with standard iostreams. I'm using this line:

std::cout.imbue(std::locale(std::locale("C"), new rot13codecvt()));

rot13codecvt is just an example, shamelessly lifted verbatim from the interwebs. A full runnable example is here. My real codecvt does something different.

As you can see in the linked live example, this doesn't quite work in g++. The output is not rot13d. No function from the custom codecvt class is ever called. But it works well in VS2012.

I have tried other locales beside "C" and none are working. Also tried an example here, same result.

What am I doing wrong?


Solution

  • Only file streams are required to use std::codecvt<...> and there is no requirement that any of the standard stream objects is implemented in terms of file streams. There are reasons for the implementers of either choice. Dinkumware's implementation uses <stdio.h> for most of its operations and it makes sense to use the same implementation under the hood in this case. libstdc++ avoids some overheads and directly accesses a buffer shared between the standard C and C++ streams and, thus, uses a different stream implementation.

    When using file streams use of the std::codecvt<...> facets should be consistent.