0
votes

I am writing a program in c++ using gcc. I have an utf8 coded source file and I want string literals from my source file as UTF-16 strings. I use the gcc option -municode, which defines the UNICODE macro. String literals are still implemented as const char *. When I use TEXT("hello world"), it is still a const char *

Also, I would like to know which macro to define, or which commandline option to use, to have 16 bit traits in the std::string objects and al the template instantiations of the std library (I want std::char_traits to be of wchar_t.

1
Macros not necessary. More here: en.cppreference.com/w/cpp/language/string_literaluser4581301
I have used different sorts of string litteral modifiers like U, u, r. It is ignored by the compilerbert-jan
Odd. What compiler?user4581301
I use g++ (minGW 64-bits edition on windows. The string litteral modifiers seem to work OK, I was mistaking. sorrybert-jan
I thought gcc used 32-bit wchar_t rather than 16-bit as Microsoft does? In that case there may be no way to create UTF-16 literals.Mark Ransom

1 Answers

1
votes

Starting with C++11, there are ways to specify UTF-16 for both string literals and string objects. It introduces char16_t for UTF-16 characters, std::u16string for UTF-16 strings, and the u prefix for UTF-16 literals.

std::u16string str = u"Hello World";

The compiler is responsible for converting the characters in your source code to the appropriate literal character values.