Is a wide character string literal starting with L like L”Hello World” guaranteed to be encoded in Unicode?

The L symbol in front of a string literal simply means that each character in the string will be stored as a wchar_t. But this doesn’t necessarily imply Unicode. For example, you could use a wide character string to encode GB 18030, a character set used in China which is similar to Unicode. The C++03 standard doesn’t have anything to say about Unicode, (however C++11 defines Unicode char types and string literals) so it’s up to you to properly represent Unicode strings in C++03.

Regarding string literals, Chapter 2 (Lexical Conventions) of the C++ standard mentions a “basic source character set”, which is basically equivalent to ASCII. So this essentially guarantees that "abc" will be represented as a 3-byte string (not counting the null), and L"abc" will be represented as a 3 * sizeof(wchar_t)-byte string of wide-characters.

The standard also mentions “universal-character-names” which allow you to refer to non-ASCII characters using the \uXXXX hexadecimal notation. These “universal-character-names” usually map directly to Unicode values, but the standard doesn’t guarantee that they have to. However, you can at least guarantee that your string will be represented as a certain sequence of bytes by using universal-character-names. This will guarantee Unicode output provided the runtime environment supports Unicode, has the appropriate fonts installed, etc.

As for string literals in C++03 source files, again there is no guarantee. If you have a Unicode string literal in your code which contains characters outside of the ASCII range, it is up to your compiler to decide how to interpret these characters. If you want to explicitly guarantee that the compiler will “do the right thing”, you’d need to use \uXXXX notation in your string literals.

Leave a Comment