Reading C++17 draft §6.9.1/5:
Types
char16_t
andchar32_t
denote distinct types with the same size, signedness, and alignment asuint_least16_t
anduint_least32_t
, respectively, in<cstdint>
, called the underlying types.
Now referring to C11 draft §7.20.1.2/2, which is the reference for the C library inheritance:
The typedef name
uint_leastN_t
designates an unsigned integer type with a width of at least N , such that no unsigned integer type with lesser size has at least the specified width. Thus,uint_least16_t
denotes an unsigned integer type with a width of at least 16 bits.
Note the "at least" part. This means that char16_t
may actually have e.g. 32 bits, making an array of char16_t
a bad representation of a UTF-16 raw data. In this case writing such an array to a binary file would result in valid code units alternating with U+0000 characters.
Is there a good reason for char16_t
to be defined in terms of uint_least16_t
instead of uint16_t
? Or is it simply a defect in the standard?