我可以为任何 C++ 编译器假设 (bool)true == (int)1 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2725044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:30:18  来源:igfitidea点击:

Can I assume (bool)true == (int)1 for any C++ compiler?

c++castingboolean

提问by Petruza

Can I assume (bool)true == (int)1for any C++ compiler ?

我可以假设(bool)true == (int)1任何 C++ 编译器吗?

回答by CB Bailey

Yes. The casts are redundant. In your expression:

是的。演员表是多余的。在你的表达中:

true == 1

Integral promotion applies and the bool value will be promoted to an intand this promotion must yield 1.

积分提升适用,bool 值将提升为 anint且此提升必须产生 1。

Reference: 4.7 [conv.integral] / 4: If the source type is bool... trueis converted to one.

参考:4.7 [conv.integral] / 4:如果源类型是bool...true则转换为1。

回答by Jerry Coffin

Charles Bailey's answer is correct. The exact wording from the C++ standard is (§4.7/4): "If the source type is bool, the value false is converted to zero and the value true is converted to one."

查尔斯·贝利的回答是正确的。C++ 标准的确切措辞是(第 4.7/4 节):“如果源类型为 bool,则值 false 将转换为零,而值 true 则转换为 1。”

Edit: I see he's added the reference as well -- I'll delete this shortly, if I don't get distracted and forget...

编辑:我看到他也添加了参考资料——我很快就会删除它,如果我不分心并忘记......

Edit2: Then again, it is probably worth noting that while the Boolean values themselves always convert to zero or one, a number of functions (especially from the C standard library) return values that are "basically Boolean", but represented as ints that are normally only required to be zero to indicate false or non-zero to indicate true. For example, the is* functions in <ctype.h>only require zero or non-zero, not necessarily zero or one.

EDIT2:话又说回来,它可能是值得一提的是,虽然布尔值本身总是转换为0或1,多项功能(尤其是来自C标准库)的返回值是“基本布尔”,但表示为intS中的通常只需要为零表示假或非零表示真。例如,is* 函数中<ctype.h>只需要零或非零,不一定为零或一。

If you cast that to bool, zero will convert to false, and non-zero to true (as you'd expect).

如果您bool将其转换为,则零将转换为 false,非零将转换为 true(如您所料)。

回答by Franci Penov

According to the standard, you should be safe with that assumption. The C++ booltype has two values - trueand falsewith corresponding values 1 and 0.

根据标准,您应该放心使用该假设。C++bool类型有两个值 -true以及false相应的值 1 和 0。

The thing to watch about for is mixing boolexpressions and variables with BOOLexpression and variables. The latter is defined as FALSE = 0and TRUE != FALSE, which quite often in practice means that any value different from 0 is considered TRUE.

需要注意的是将bool表达式和变量与BOOL表达式和变量混合在一起。后者被定义为FALSE = 0and TRUE != FALSE,这在实践中通常意味着考虑任何不同于 0 的值TRUE

A lot of modern compilers will actually issue a warning for any code that implicitly tries to cast from BOOLto boolif the BOOLvalue is different than 0 or 1.

如果值不同于 0 或 1,许多现代编译器实际上会针对任何隐式尝试从BOOLto强制转换的代码发出警告。boolBOOL

回答by Michael Dorgan

I've found different compilers return different results on true. I've also found that one is almost always better off comparing a bool to a bool instead of an int. Those ints tend to change value over time as your program evolves and if you assume true as 1, you can get bitten by an unrelated change elsewhere in your code.

我发现不同的编译器在 true 上返回不同的结果。我还发现将 bool 与 bool 而不是 int 进行比较几乎总是更好。随着您的程序的发展,这些整数往往会随着时间的推移而改变值,如果您假设 true 为 1,您可能会被代码中其他地方的无关更改所困扰。