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

标签 c++ boolean sizeof implementation-defined-behavior

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

最佳答案

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

§5.3.3/1,删节:

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) is not required to be 1.

关于c++ - sizeof(bool) 是否在 C++ 语言标准中定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4897844/

相关文章:

c++ - 检测按键按下和按键释放事件

c - 我不能使用 <search.h> 中的 sizeof(struct hsearch_data)

c++ - 结合 std::wstring 和函数

c++ - OnPaint MFC 上不显示矩形

java - 是否建议在 java 中使用 PreparedStatement.setBoolean(1, Boolean.TRUE)?

java - 使用默认 ObjectMapper 后 boolean JSON 参数为 false

c# - 为什么结构的 sizeof 是不安全的

c++ - 3 字节 int 和 5 字节长?

c++ - 编译期间 MSVC 中的字符串长度限制?

java - Boolean.valueOf() 的良好用法