Search code examples
c++unicodeutf-8utf-16utf-32

Does std::wstring support UTF-16 and UTF-32 on Windows?


I'm learning about Unicode and have a few questions that I'm hoping to get answered.

1) I've read that on Linux, a std::wstring is 4-bytes, while on Windows, it's 2-bytes. Does this mean that Linux internal support is UTF-32 while Windows it is UTF-16?

2) Is the use of std::wstring very similar to the std::string interface?

3) Does VC++ offer support for using a 4-byte std::wstring?

4) Do you have to change compiler options if you use std::wstring?

As a sidenote, I came across a string library for working with UTF-8 which has a very similar interface to std::string which provides familiar functionality such as length, substr, find, upper/lower case conversion etc. The library is Glib::ustring.

Please feel free to add any comments or additional advice, because I really need it.

Thank you!


Solution

  • 1) wstring is a basic_string<wchar_t> and the size of wchar_t is implementation dependent and encoding agnostic (the standard just says that "its values can represent distinct codes for all members of the largest extended character set specified among the supported locales". But yes, an implementation that has sizeof(wchar_t)=4 bytes supports UTF-32, and sizeof(wchar_t)=2 bytes supports UTF-16.

    2) wstring is a basic_string<wchar_t> whereas string is a basic_string<char>, so yes, it is a very similar interface. You will have to use wcout, wcin and wfstream though, and have some other constraints like this.

    3) No, MSVC defines wchar_t as unsigned short, which defines and limits wstring as you said. MSVC gives possibility of handling wchar_t as a typedef instead of an internal type. You could imagine then to redefine the typedef, but I suspect this is extreamly risky and evil.

    4) No, it's up to you to choose to the string type you want.

    5) UTF-32 and the standard : Interestingly, in the very encoding agnostic C++ standard, UTF-32 is mentionned explicitely only for codecvt: "the specialization codecvt <char32_t, char, mbstate_t> converts between the UTF-32 and UTF-8 encoding forms. codecvt converts between the native character sets for narrow and wide characters." This suggests that char32_t would be the portable approach to UTF-32. Unfortunately MSVC doesn't support this type yet.