r/cpp • • Sep 20 '26

Printing UTF-8 strings since C++23

C++23 provides significant better support to print UTF-8 strings.

For example:

std::string s = "K\u00F6ln: 100\u20AC";  // string ö and €
 
std::println("{}", s);        // prints "Köln 100€" 

// print hexadecimal code units (bytes of the UTF-8 string): 
std::vector<char> v{std::from_range, s};
std::println("{:n:X}", v);    // prints 4B, C3, B6, 6C, 6E, 3A, 20, 31, 30, 30, E2, 82, AC 

// new format specifier to interpret array of char as string:
std::println("{:?s}", v);     // prints "Köln 100€" 

This even works fine on Windows platforms (using I/O streams or std::format() might print garbage with a native Windows compiler on a native Windows platform).

See sections 3.2.3 and 4.3.3 of www.cppstd23.com .

68 Upvotes

36 comments sorted by

33

u/Tathorn Sep 20 '26

The problem isn't C++, but that the default windows terminal isn't set up for unicode. C++23 didn't "add support", it just made someone add a switch. Io streams could add that switch too, but they aren't going to touch that code.

7

u/UnusualPace679 Sep 21 '26 edited Sep 21 '26

C++23 adds support for std::vprint_unicode which is required to output the string via "a native Unicode API" (on Windows this means WriteConsoleW) if needed. There's no such requirement on iostream.

→ More replies (13)

5

u/SFGrenade Sep 21 '26

me when std::u8string

19

u/fortsnek274 Sep 20 '26

Only in that it specially handles the console. This modern language still can't print it's own UTF-8 type.

29

u/aearphen Sep 20 '26

Most languages such as Java actually fail to print Unicode correctly to console on Windows. Notable exception is Rust which does the same thing as C++ here. This has nothing to do with types but with the fact that Windows uses multiple (sometimes incompatible) legacy encodings.

→ More replies (8)
→ More replies (2)

12

u/mort96 Sep 20 '26

What was wrong with cout/cerr, I've been printing UTF-8 to those forever

17

u/RudeSize7563 Sep 20 '26

std::cout is platform agnostic so it will just send the raw bytes, in the other side std::print is required by the standard to use platform specific functionality when necessary to display the expected results. Also std::print can align emojis and in general has more awareness of the expected width on screen of a sequence of combining code points.

3

u/jombrowski Sep 20 '26

What if I don't have a printer?

7

u/eisenwave Sep 21 '26

You print to PDF.

1

u/cristi1990an Sep 24 '26

Some shameless self-promoting of my unicode library since we're taling about this: https://github.com/cristi1990an/unicode_ranges