Skip to content

Trivia impact: real

The UTF-8 Literal That Is char

C++17's UTF-8 prefix does not yet create a new character type

auto text = u8"tea";
std::cout << std::boolalpha
          << std::is_same<decltype(text), const char*>::value << '\n';

Does the u8 prefix make this a pointer to a special C++17 character type?

Answer
true

In C++17, the literal decays to const char*.

Why

[lex.string] gives a UTF-8 string literal the element type char in C++17. The auto declaration performs the usual array-to-pointer conversion, hence the observed const char* type. C++20 changed the element type of u8 literals to char8_t, so code that treats C++17 UTF-8 literals as ordinary char strings can need adjustment.

Where it shows up

It matters at boundaries between UTF-8 text and APIs that accept char*, especially in projects compiling some targets as C++17 and others as C++20. The prefix specifies an encoding, not Unicode-aware string operations.

Takeaway: In C++17, u8"..." is a char string literal; in C++20 it uses char8_t.

Try it: g++ -std=c++17 main.cpp -o demo && ./demo

Open in Compiler Explorer ↗ Quiz this entry