How to deal with Unicode strings in C/C++ in a cross-platform friendly way?

cross-platform, string, tchar, unicode, windows

Solution

I can only suggest you to check this library out: http://cppcms.sourceforge.net/boost_locale/docs/ It might help, it's a boost candidate for now but I believe it will make it.

Problem

On platforms different than Windows you could easily use `char *` strings and treat them as UTF-8. The problem is that on Windows you are required to accept and send messages using wchar* strings (W). If you'll use the ANSI functions (A) you will not support Unicode. So if you want to write truly portable application you need to compile it as Unicode on Windows. Now, In order to keep the code clean I would like to see what is the recommended way of dealing with strings, a way that minimize ugliness in the code. Type of strings you may need: `std::string`, `std::wstring`, `std::tstring`,`char *`,`wchat_t *`, `TCHAR*`, `CString` (ATL one). Issues you may encounter: - `cout/cerr/cin` and their Unicode variants `wcout,wcerr,wcin` - all renamed wide string functions and their TCHAR macros - like `strcmp`, `wcscmp` and `_tcscmp`. - constant strings inside code, with TCHAR you will have to fill your code with `_T()` macros. What approach do you see as being best? (examples are welcome) Personally I would go for a `std::tstring` approach but I would like to see how would do to the conversions where they are necessary.

Original source