sizeof(bool) 是否在 C++ 语言标准中定义?

我在标准文档中找不到答案.C++ 语言标准是否要求 sizeof(bool) 始终为 1(对于 1 个字节),还是这个大小是实现定义的?

I can't find an answer in the standard documentation. Does the C++ language standard require sizeof(bool) to always be 1 (for 1 byte), or is this size implementation-defined?

推荐答案

sizeof(bool) 是实现定义的,标准特别强调了这一点.

sizeof(bool) is implementation defined, and the standard puts notable emphasis on this fact.

§5.3.3/1,删节:

§5.3.3/1, abridged:

sizeof(char)sizeof(signed char)sizeof(unsigned char)为1;sizeof 应用于任何其他基本类型的结果是实现定义的.[注意:特别是 sizeof(bool)sizeof(wchar_t) 是实现定义的.69)]

sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the result of sizeof applied to any other fundamental type is implementation-defined. [Note: in particular, sizeof(bool) and sizeof(wchar_t) are implementation-defined.69)]

脚注 69):

sizeof(bool) 不需要为 1.

相关文章