Skip to main content
char8_t is a fundamental character type introduced in C++20 specifically designed to represent UTF-8 encoded code units. It provides strict type safety for UTF-8 data at the compiler level, distinguishing it from standard narrow character types (char, signed char, and unsigned char).

Type Properties

  • Size: sizeof(char8_t) is always 1 byte.
  • Underlying Type: It shares the exact size, signedness, and alignment requirements of unsigned char.
  • Distinct Type: Unlike int8_t or uint8_t (which are typically typedef aliases), char8_t is a distinct built-in type. This allows it to participate uniquely in function overload resolution and template specialization.

Syntax and Literals

The u8 prefix dictates that a character or string literal is of type char8_t or an array of const char8_t, respectively.

Standard Library Aliases

The C++ Standard Library provides specific aliases for strings and string views utilizing char8_t as the underlying character type:

Type Safety and Conversions

Because char8_t is a distinct type, the strict type system prevents accidental mixing of UTF-8 data with raw bytes or locally-encoded strings by disabling implicit conversions.
Integral promotion rules apply to char8_t identically to unsigned char. When used in arithmetic or bitwise operations, a char8_t value is implicitly promoted to int (or unsigned int if int cannot represent the entire range of the underlying type on the target architecture).

Standard Stream I/O

To prevent mojibake (corrupted text rendering caused by mismatched encodings), C++20 restricts direct streaming of UTF-8 types to standard character streams like std::cout. The standard explicitly deletes the stream insertion operator (operator<<) for char8_t characters and pointers (e.g., const char8_t*). This explicit deletion prevents the compiler from falling back to implicit conversions that would match other overloads, such as void* or integral types. Conversely, for std::u8string, the standard simply does not provide an operator<< overload for std::ostream (std::basic_ostream<char>). Attempting to stream it results in a template deduction failure rather than a deleted function error.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More