Skip to main content

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.

char16_t is a fundamental, distinct character type in C++ introduced in C++11, specifically designed to represent 16-bit wide characters and serve as the standard language-level type for UTF-16 encoded code units.

Type Characteristics

  • Underlying Representation: char16_t is guaranteed by the C++ Standard to have the exact same size, signedness, and alignment requirements as std::uint_least16_t (defined in <cstdint>).
  • Signedness: It is strictly an unsigned type.
  • Size: It is guaranteed to be at least 16 bits wide. Because the sizeof operator returns the size in C++ bytes (where one byte is defined as CHAR_BIT bits), sizeof(char16_t) is not strictly guaranteed to be 2. On systems where CHAR_BIT is 8, sizeof(char16_t) is 2; however, on systems where CHAR_BIT is 16 or greater, sizeof(char16_t) evaluates to exactly 1.
  • Type Distinctness: char16_t is a distinct type, not a typedef. While it shares the memory layout of std::uint_least16_t, it is treated as a unique type by the compiler for the purposes of function overloading and template instantiation.
  • Platform Consistency: Unlike wchar_t, whose size is implementation-defined (typically 16 bits on Windows and 32 bits on POSIX systems), char16_t provides a strict cross-platform guarantee regarding its minimum bit width and intended encoding.

Syntax and Literals

To declare a char16_t character or string literal, prefix the literal with a lowercase u.
// Character literal (must fit within a single 16-bit code unit)
char16_t ascii_char = u'A';
char16_t unicode_char = u'\u00DF'; // U+00DF (ß)

// String literal (deduced as an array of const char16_t)
const char16_t* str = u"UTF-16 encoded string literal";

// Surrogate pairs (characters outside the Basic Multilingual Plane)
// require two char16_t code units, so they cannot be stored in a single char16_t.
// They must be stored in an array or string.
const char16_t* emoji = u"\U0001F600"; // U+1F600 (😀) - takes two char16_t elements

Standard Library Integration

The C++ Standard Library provides specialized aliases and traits to support char16_t operations, primarily found in the <string> and <string_view> headers.
#include <string>
#include <string_view>

// Alias for std::basic_string<char16_t, std::char_traits<char16_t>, std::allocator<char16_t>>
std::u16string dynamic_str = u"Managed UTF-16 string";

// Alias for std::basic_string_view<char16_t, std::char_traits<char16_t>> (C++17)
std::u16string_view str_view = u"Non-owning UTF-16 string view";
Standard stream objects (like std::cout) do not have overloaded insertion operators (<<) for char16_t or std::u16string. Outputting these types directly requires explicit conversion to a supported character type (like char) or the use of standard library conversion facets.
Master C++ with Deep Grasping Methodology!Learn More