Is sizeof(bool) defined in the C++ language standard?

送分小仙女□ 提交于 2019-11-26 00:38:13

问题


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?


回答1:


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

§5.3.3/1, abridged:

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)]

Footnote 69):

sizeof(bool) is not required to be 1.




回答2:


http://msdn.microsoft.com/en-us/library/tf4dy80a.aspx

"In Visual C++4.2, the Standard C++ header files contained a typedef that equated bool with int. In Visual C++ 5.0 and later, bool is implemented as a built-in type with a size of 1 byte. That means that for Visual C++ 4.2, a call of sizeof(bool) yields 4, while in Visual C++ 5.0 and later, the same call yields 1. This can cause memory corruption problems if you have defined structure members of type bool in Visual C++ 4.2 and are mixing object files (OBJ) and/or DLLs built with the 4.2 and 5.0 or later compilers."




回答3:


It's implementation defined. Only sizeof(char) is 1 by the standard.




回答4:


See 5.3.3 paragraph 1 :

[Note: in particular, sizeof(bool) and sizeof(wchar_t) are implementation-defined.69) ]



来源:https://stackoverflow.com/questions/4897844/is-sizeofbool-defined-in-the-c-language-standard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!