保证在char,wchar_t,char16_t,char32_t上的大小顺序 [英] Guarantee on size ordering on char, wchar_t, char16_t, char32_t
问题描述
C ++标准是否对以char
,wchar_t
,char16_t
,char32_t
为单位的字节大小排序提供任何保证? (欢迎从标准中提取任何内容)
Does the C++ standard provide any guarantee on the ordering of the size in bytes of char
, wchar_t
, char16_t
, char32_t
? (any extract from the standard is welcome)
例如,我是否保证:
sizeof(char) <= sizeof(wchar_t) <= sizeof(char16_t) <= sizeof(char32_t)
推荐答案
它是1 == sizeof(char) <= sizeof(wchar_t)
和1 == sizeof(char) <= sizeof(char16_t) <= sizeof(char32_t)
.
5.3.3/1 Sizeof [expr.sizeof]
5.3.3/1 Sizeof [expr.sizeof]
... sizeof(char),sizeof(signed char)和sizeof(unsigned char)为1. ... [注意:尤其是sizeof(bool),sizeof(char16_t), 75-实现中定义了sizeof(char32_t)和sizeof(wchar_t). 结束语].
... 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]
3.9.1/5 Fundamental types [basic.fundamental]
...类型wchar_t必须具有相同的大小,符号和对齐方式 要求(3.11)作为其他整数类型之一,称为 基础类型.类型char16_t和char32_t表示不同的类型 具有与uint_least16_t相同的大小,符号和对齐方式,并且 < cstdint>中的uint_least32_t分别称为基础 类型.
... 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
:
Update: I haven't found it in the standard. cppreference says for uint_leastN_t
:
最小无符号整数类型,宽度至少为8、16、32和64 位
smallest unsigned integer type with width of at least 8, 16, 32 and 64 bits respectively
请注意,sizeof(char)== 1并不表示 表示char有8位.另请参见 C ++常见问题解答. cppreference 谈到CHAR_BIT:
Note that sizeof(char)==1 does not mean that a char has 8 bits. See also C++ FAQ. cppreference says about CHAR_BIT:
字节数
1.7/1 C ++内存模型[intro.memory]
1.7/1 The C ++ memory model [intro.memory]
C ++内存模型中的基本存储单元是字节.一种 字节至少要大到足以包含基本的任何成员 执行字符集(2.3)...
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) ...
这篇关于保证在char,wchar_t,char16_t,char32_t上的大小顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!