[英]Are C++ bools converted to ints during comparison?
When using comparison operators in C++, are bools converted to ints? 在C ++中使用比较运算符时,bool是否转换为整数?
The reason I ask is that the question of whether or not to always explicitly compare to true/false in if-statements came up. 我问的原因是在if语句中是否始终明确地与true / false比较的问题出现了。 The two options being: 这两个选项是:
1) if (my_bool == true) doSomething();
2) if (my_bool) doSomething();
We were thinking you should generally avoid the explicit comparison (1) because of the following: 我们认为你应该通常避免明确的比较(1),因为以下内容:
int myFunc(){return 4;}
if (myFunc() == true) doSomething();
Something like the code above would come up if you need to work with C interfaces that simply return nonzero to indicate "true". 如果您需要使用简单地返回非零以指示“true”的C接口,则会出现类似上面代码的内容。 The myFunc()
example would fail in C because myFunc
returns 4, true
is macro'd to 1, and 4 == 1
is not true. myFunc()
示例在C中会失败,因为myFunc
返回4, true
为宏,为1,而4 == 1
不为真。
Is this still the case in C++? 在C ++中仍然如此吗? Does the "equal to" operator convert the bool to an int rather than the other way around? “等于”运算符是否将bool转换为int而不是相反? References to the standard (C++11 is what I'm working with) are appreciated, but if it differs among versions of the language I'd be interested in knowing. 我很欣赏对标准的引用(C ++ 11是我正在使用的),但是如果它在我不想知道的语言版本之间有所不同。
(I'd like to ask specifically about the pros/cons of explicit true/false comparison, but that seems like it could be subjective.) (我想特别询问明确的真/假比较的利弊,但这看起来似乎是主观的。)
When using comparison operators in C++, are bools converted to ints? 在C ++中使用比较运算符时,bool是否转换为整数?
Yes. 是。 For relational operators ([expr.rel]/5): 对于关系运算符([expr.rel] / 5):
The usual arithmetic conversions are performed on operands of arithmetic or enumeration type. 通常的算术转换是在算术或枚举类型的操作数上执行的。
For equality operators ([expr.eq]/6): 对于相等运算符([expr.eq] / 6):
If both operands are of arithmetic or enumeration type, the usual arithmetic conversions are performed on both operands; 如果两个操作数都是算术类型或枚举类型,则对两个操作数执行通常的算术转换;
When both operands are bool
, they are both promoted to int
. 当两个操作数都是bool
,它们都被提升为int
。 If one operand is a bool
and the other is of integral type, the bool
operand is promoted to int
. 如果一个操作数是bool
而另一个是整数类型,则bool
操作数被提升为int
。 See [expr]/10: 见[expr] / 10:
Otherwise, the integral promotions (4.5) shall be performed on both operands. 否则,应对两个操作数执行整体促销(4.5)。
As far as I know, this has been true since the beginning of time (it does not differ between revisions of the standard). 据我所知,从一开始就是如此(标准的修订版本没有区别)。
I do not think there is any performance implication of doing explicit comparison to true
or false
, but I wouldn't do it myself, since I consider it redundant and not in a way that yields any benefit. 我不认为对true
或false
进行明确比较有任何性能影响,但我不会自己做,因为我认为它是多余的,而不是产生任何好处的方式。
They are not converted, bool actually ARE integer types. 它们没有转换,bool实际上是ARE整数类型。
See the standard: 看标准:
"Values of type bool are either true or false. As described below, bool values behave as integral types. Values of type bool participate in integral promotions" ~ C++03 “bool类型的值是true还是false。如下所述,bool值表现为整数类型.bool类型的值参与整体促销”~C ++ 03
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.