一个bool变量可以存储超过0x01吗? [英] Can a bool variable store more than 0x01?
问题描述
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bool a = 0x03;
bitset<8> x(a);
cout<<x<<endl;
a = a>>1;
bitset<8> y(a);
cout<<y<<endl;
}
结果是:
00000001
00000000
不::
The result is not:
00000011
00000001
如果我从 bool $ c类型更改
a
$ c>到 char
,结果将是第二个。
这意味着我不能在 bool
中存储超过0x01,所有正确的值大于0x01被视为0x01。
If I change the type of a
from bool
to char
, the result will be the second one.
It means that I cannot store more than 0x01 in a bool
, all right value greater than 0x01 are treat as 0x01.
所有编译器都有此行为?
All compiler has the behavior?
推荐答案
§4.12布尔转换[conv.bool]
§4.12 Boolean conversions [conv.bool]
1算术,无范围枚举,指针或指向成员类型的指针的prval值可以转换为bool类型的prvalue。 零值,空指针值或空成员指针值转换为false ; 任何其他值转换为true 。类型std :: nullptr_t的prvalue可以转换为bool类型的prvalue;则结果值为false。
1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.
这篇关于一个bool变量可以存储超过0x01吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!