保证 char、wchar_t、char16_t、char32_t 上的大小排序

Guarantee on size ordering on char, wchar_t, char16_t, char32_t

C++ 标准是否对 charwchar_tchar16_tchar32_t 的字节大小排序提供任何保证? (欢迎从标准中提取任何内容)

例如,我是否可以保证:

sizeof(char) <= sizeof(wchar_t) <= sizeof(char16_t) <= sizeof(char32_t)

类型 char16_tchar32_t 分别定义为与 uint_least16_tuint_least32_t 大小相同。

wchar_t 上不存在此类限制,除了它必须至少与 char 一样大(当然,all C 和 C++ 中的数据类型)。存在具有 1 字节、2 字节 (MSVC++) 和 4 字节 (GCC) 宽字符的实际实现。

1 == sizeof(char) <= sizeof(wchar_t)1 == sizeof(char) <= sizeof(char16_t) <= sizeof(char32_t)

5.3.3/1 大小[expr.sizeof]

... sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1. ... [ Note: in particular, sizeof(bool), sizeof(char16_t), sizeof(char32_t), and sizeof(wchar_t) are implementation-defined.75 — end note ].

3.9.1/5 基本类型 [basic.fundamental]

... Type wchar_t shall have the same size, signedness, and alignment requirements (3.11) as one of the other integral types, called its underlying type. Types char16_t and char32_t denote distinct types with the same size, signedness, and alignment as uint_least16_t and uint_least32_t, respectively, in <cstdint>, called the underlying types.

更新:我没有在标准中找到它。 cppreference 表示 uint_leastN_t:

smallest unsigned integer type with width of at least 8, 16, 32 and 64 bits respectively

请注意 sizeof(char)==1 确实 而不是 意味着 char 有 8 位。另见 C++ FAQ. cppreference 关于 CHAR_BIT 的说法:

number of bits in byte

1.7/1 C++内存模型[intro.memory]

The fundamental storage unit in the C ++ memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set (2.3) ...

在 64 位 Qt Creator(使用 Clang)中,wchar_t 是四 (4) 个字节。