Imagine I have decided to use UTF-8 everywhere internally in my C++11 program, so I have a std::string
that contains text encoded in UTF-8. I now want to do some IO of that text. Writing it to std::cout
, for example. Although I've used UTF-8 internally, I can not assume the program user and operating environment is so obliging as to use UTF-8 too. For good or bad reasons, the character encoding of text that I ought to send through std::cout
might not be UTF-8. My program must perform a conversion, taking my UTF-8 encoded text and converting it to the encoding that std::cout
expects. How can I find out the encoding on that output stream, then do the character encoding?
Looking at the declarations of standard C++ streams, it looks like I can use std::io_base::get_loc
to get the "locale" of the output stream, then get a std::codecvt
"code conversion facet" for the stream. But which facet should I get? And how do I actually use that facet to convert from UTF-8 to the output encoding?
And if those facilities of the standard library can not do the task, what other options do I have?