Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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 always1byte. - Underlying Type: It shares the exact size, signedness, and alignment requirements of
unsigned char. - Distinct Type: Unlike
int8_toruint8_t(which are typicallytypedefaliases),char8_tis a distinct built-in type. This allows it to participate uniquely in function overload resolution and template specialization.
Syntax and Literals
Theu8 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 utilizingchar8_t as the underlying character type:
Type Safety and Conversions
Becausechar8_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.
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 likestd::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.
Master C++ with Deep Grasping Methodology!Learn More





