示例波纹管编译,但输出相当奇怪:
#include <iostream>
#include <cstring>
struct A
{
int a;
char b;
bool c;
};
int main()
{
A v;
std::memset( &v, 0xff, sizeof(v) );
std::cout << std::boolalpha << ( true == v.c ) << std::endl;
std::cout << std::boolalpha << ( false == v.c ) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
true
true
Run Code Online (Sandbox Code Playgroud)
有人能解释为什么吗?
如果重要,我使用的是g ++ 4.3.0
Rom*_*n L 11
在C++标准的第3.9.1节"基本类型"中找到了这一点(注意魔术脚注42):
6. Values of type bool are either true or false. 42)
Run Code Online (Sandbox Code Playgroud)
42)以本国际标准描述的方式将bool值用作"未定义",例如通过检查未初始化的自动变量的值,可能会使其表现为既不是真也不是假.
这对我来说并不是很清楚,但似乎回答了这个问题.