C++中bool数据类型的比较
Comparison of bool data types in C++
bool
数据类型通常表示为 0
(如 false
)和 1
(如 true
)。但是,有人说 true
值可以用 1
以外的值表示。如果后面的语句是true
,那么下面的表达式可能不正确。
bool x = 1;
if (x==1)
Do something..
我想知道以下语句是否会在常用编译器上按预期工作。
-
bool x = 1;
if (x==1)
Do something.
-
bool y = 0;
if (y>0.5)
Do something..
-
bool z = 1;
if(z>0.5)
Do something...
if (x==1)
没有错。当您将布尔值转换为数字类型时,所有真值表示都转换为 1。
给定 bool z=true
,if(z>0.5)
将为真,因为 1.0 大于 0.5。
C++ standard 的§4.5 说:
An rvalue of type bool can be
converted to an rvalue of type int,
with false becoming zero and true
becoming one.
关于 2 和 3,发生了类型转换,因此语句将按预期工作
根据Boolean conversions的规则:
A prvalue of integral, floating-point, unscoped enumeration, pointer,
and pointer-to-member types can be converted to a prvalue of type
bool
.
The value zero (for integral, floating-point, and unscoped
enumeration) and the null pointer and the null pointer-to-member
values become false
. All other values become true
.
然后
bool x = 1; // x will be true
bool y = 0; // y will be false
bool z = 1; // z will be true
对于第一种情况,if (x==1)
、x
将是 promoted 到 int
、
the type bool
can be converted to int
with the value false
becoming 0
and true
becoming 1
.
那么 (x==1)
就是 true
.
对于第二种情况,if (y>0.5)
,y
将提升为int
,值为0
,然后converted to double
for the comparison;
If the operands has arithmetic or enumeration type (scoped or unscoped), usual arithmetic conversions are performed on both operands following the rules for arithmetic operators. The values are compared after conversions:
和
If the operand passed to an arithmetic operator is integral or unscoped enumeration type, then before any other action (but after lvalue-to-rvalue conversion, if applicable), the operand undergoes integral promotion.
...
- Otherwise, if either operand is
double
, the other operand is converted to double
那么y>0.5
就是false
。
对于第三种情况,if (z>0.5)
,z
会被提升为int
,值为1
,然后converted to double
for the comparison;那么 z>0.5
就是 true
.
bool
只有两个值,分别是true
和false
。 1
和 0
是整数文字,因此它们可以转换为 bool
。你必须考虑到转换是双向的,但你不一定会得到相同的整数:
int a = 5;
bool b = a; // int -> bool conversion
int c = b; // bool -> int conversion
std::cout << a << " " c;
打印:
5 1
除 0
以外的任何整数值都会转换为 true
,但 true
始终会转换为 1
。
牢记这一点,您的所有示例都将按预期工作。但是,请注意 bool
的主要目的是我们可以在我们的代码中使用 true
和 false
而不必将数字作为 0
和 1
特殊意义。明确总是更好,所以当你的意思是 true
时,你最好写 true
而不是 1
.
这里的一个相关问题是“为什么我们甚至允许比较 bool
和 int
?”。
答案是向后兼容和折衷,因为C和C++曾经推荐整数来存储布尔值。所以有很多代码
- 尽管有新的语言规则,仍应继续工作,这样
关系运算符 return 一个布尔值而不是一个整数。
- 允许您通过更改布尔变量的声明来升级旧代码
对其余代码进行最少的后续更改。
其他语言在这方面可能更简洁,但 bool
类型显然是成功的。
bool
数据类型通常表示为 0
(如 false
)和 1
(如 true
)。但是,有人说 true
值可以用 1
以外的值表示。如果后面的语句是true
,那么下面的表达式可能不正确。
bool x = 1;
if (x==1)
Do something..
我想知道以下语句是否会在常用编译器上按预期工作。
-
bool x = 1; if (x==1) Do something.
-
bool y = 0; if (y>0.5) Do something..
-
bool z = 1; if(z>0.5) Do something...
if (x==1)
没有错。当您将布尔值转换为数字类型时,所有真值表示都转换为 1。
给定 bool z=true
,if(z>0.5)
将为真,因为 1.0 大于 0.5。
C++ standard 的§4.5 说:
An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one.
关于 2 和 3,发生了类型转换,因此语句将按预期工作
根据Boolean conversions的规则:
A prvalue of integral, floating-point, unscoped enumeration, pointer, and pointer-to-member types can be converted to a prvalue of type
bool
.The value zero (for integral, floating-point, and unscoped enumeration) and the null pointer and the null pointer-to-member values become
false
. All other values becometrue
.
然后
bool x = 1; // x will be true
bool y = 0; // y will be false
bool z = 1; // z will be true
对于第一种情况,if (x==1)
、x
将是 promoted 到 int
、
the type
bool
can be converted toint
with the valuefalse
becoming 0
andtrue
becoming1
.
那么 (x==1)
就是 true
.
对于第二种情况,if (y>0.5)
,y
将提升为int
,值为0
,然后converted to double
for the comparison;
If the operands has arithmetic or enumeration type (scoped or unscoped), usual arithmetic conversions are performed on both operands following the rules for arithmetic operators. The values are compared after conversions:
和
If the operand passed to an arithmetic operator is integral or unscoped enumeration type, then before any other action (but after lvalue-to-rvalue conversion, if applicable), the operand undergoes integral promotion.
...
- Otherwise, if either operand is
double
, the other operand is converted todouble
那么y>0.5
就是false
。
对于第三种情况,if (z>0.5)
,z
会被提升为int
,值为1
,然后converted to double
for the comparison;那么 z>0.5
就是 true
.
bool
只有两个值,分别是true
和false
。 1
和 0
是整数文字,因此它们可以转换为 bool
。你必须考虑到转换是双向的,但你不一定会得到相同的整数:
int a = 5;
bool b = a; // int -> bool conversion
int c = b; // bool -> int conversion
std::cout << a << " " c;
打印:
5 1
除 0
以外的任何整数值都会转换为 true
,但 true
始终会转换为 1
。
牢记这一点,您的所有示例都将按预期工作。但是,请注意 bool
的主要目的是我们可以在我们的代码中使用 true
和 false
而不必将数字作为 0
和 1
特殊意义。明确总是更好,所以当你的意思是 true
时,你最好写 true
而不是 1
.
这里的一个相关问题是“为什么我们甚至允许比较 bool
和 int
?”。
答案是向后兼容和折衷,因为C和C++曾经推荐整数来存储布尔值。所以有很多代码
- 尽管有新的语言规则,仍应继续工作,这样 关系运算符 return 一个布尔值而不是一个整数。
- 允许您通过更改布尔变量的声明来升级旧代码 对其余代码进行最少的后续更改。
其他语言在这方面可能更简洁,但 bool
类型显然是成功的。