C++ UNICODE and STL C++ UNICODE and STL windows windows

C++ UNICODE and STL


In general you shouldn’t mix those two encodings. However, exception messages are something that is only of interest to the developer (e.g. in log files) and should never be shown to the user (but look at Jim’s comment for an important caveat).

So you are on the safe side if you use UNICODE for your whole user-faced interface and still use std::exception etc. behind the scenes for developer messages. There should be no need ever to convert between the two.

Furthermore, it’s a good trick to define a typedef for UNICODE-independent strings in C++:

typedef std::basic_string<TCHAR> tstring;

… and analogously define tcout, tcin etc. conditionally:

#ifdef UNICODE    std::wostream& tcout = std::wcout;    std::wostream& tcerr = std::wcerr;    std::wostream& tclog = std::wclog;    std::wistream& tcin = std::wcin;#else    std::ostream& tcout = std::cout;    std::ostream& tcerr = std::cerr;    std::ostream& tclog = std::clog;    std::istream& tcin = std::cin;#endif


Josh,

Please have a look at my answer here: https://softwareengineering.stackexchange.com/questions/102205/should-utf-16-be-considered-harmful

There is growing number of engineers who believe std::string is just perfect for unicode on Windows, and is the right way to write portable and unicode-correct programs faster.


Take a look at this (rather old now) article on CodeProject: Upgrading an STL-based application to use Unicode. It covers the issues you're likely to hit if you're using the STL extensively. It shouldn't be that bad and, generally speaking, it's worth your while to use wide strings.