I recently discovered that on Windows 10/11 there is a beta testing option under region settings (system locale) to "Use Unicode UTF-8 for worldwide language support". When this is enabled, all the ANSI Win32 system calls treat string as UTF-8. Sure enough, if enabled you can compile the following in MSVC:
int main() {
std::cout << "Hello, World! こんにちは世界!" << std::endl;
//prints "Hello, World! こんにちは世界!"
}
I then read that you don't have to enable this system-wide and can instead compile your program with the /utf-8
flag. So with the beta option disabled and the /utf-8
flag added to my project:
int main() {
std::cout << u8"Hello, World! こんにちは世界!" << std::endl;
//prints "Hello, World! こんにちは世界!"
}
and
int main() {
setlocale(LC_ALL, "en_US.utf-8");
std::cout << "Hello, World! こんにちは世界!" << std::endl;
//prints "Hello, World! ???????!"
}
I also tried adding u8
to the string literal, but it makes no difference.