C++ bool 到 int 的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5369770/
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 18:01:11  来源:igfitidea点击:

bool to int conversion

c++cc99

提问by pic11

How portable is this conversion. Can I be sure that both assertions pass?

这种转换有多便携。我能确定这两个断言都通过了吗?

int x = 4<5;
assert(x==1);

x = 4>5;
assert(x==0);

Don't ask why. I know that it is ugly. Thank you.

不要问为什么。我知道它很丑。谢谢你。

回答by Nawaz

int x = 4<5;

Completely portable. Standard conformant. boolto intconversion is implicit!

完全便携。符合标准。boolint转换是隐式的!

§4.7/4 from the C++ Standard says (Integral Conversion)

C++ 标准中的 §4.7/4 说(积分转换

If the source type is bool, the value falseis converted to zeroand the value trueis converted to one.

如果源类型为 bool ,则将值false转换为零,并将值true转换为 1



As for C, as far as I know there is no boolin C. (before 1999) So boolto intconversion is relevant in C++ only. In C, 4<5evaluates to intvalue, in this case the value is 1, 4>5would evaluate to 0.

至于C,据我所知是没有bool的C.(1999年以前),所以boolint转换在C ++中才有意义。在 C 中,4<5计算为intvalue,在这种情况下,值为14>5将计算为0

EDIT: Jens in the comment said, C99 has _Booltype. boolis a macro defined in stdbool.hheader file. trueand falseare also macro defined in stdbool.h.

编辑:Jens 在评论中说,C99 有_Bool类型。boolstdbool.h头文件中定义的宏。true并且false也是在stdbool.h.

§7.16 from C99 says,

C99 中的 §7.16 说,

The macro boolexpands to _Bool.

[..] truewhich expands to the integer constant 1, falsewhich expands to the integer constant 0,[..]

bool扩展为_Bool。

[..]true扩展为整数常量1,false扩展为整数常量0,[..]

回答by AnT

You tagged your question [C] and [C++] at the same time. The results will be consistent between the languages, but the structure of the the answer is different for each of these languages.

您同时标记了您的问题 [C] 和 [C++]。语言之间的结果将是一致的,但是对于这些语言中的每一种,答案的结构都不同。

In C language your examples has no relation to boolwhatsoever (that applies to C99 as well). In C language relational operators do not produce boolresults. Both 4 > 5and 4 < 5are expressions that produce results of type intwith values 0or 1. So, there's no "bool to int conversion" of any kind taking place in your examples in C.

在 C 语言中,您的示例与任何内容无关bool(也适用于 C99)。在 C 语言中,关系运算符不会产生bool结果。两者4 > 54 < 5是产生类型的结果的表达式int用值01。因此,在您的 C 示例中没有发生任何类型的“bool 到 int 转换”。

In C++ relational operators do indeed produce boolresults. boolvalues are convertible to inttype, with trueconverting to 1and falseconverting to 0. This is guaranteed by the language.

在 C++ 中,关系运算符确实会产生bool结果。bool值可以转换为int类型,true转换为1false转换为0. 这是由语言保证的。

P.S. C language also has a dedicated boolean type _Bool(macro-aliased as bool), and its integral conversion rules are essentially the same as in C++. But nevertheless this is not relevant to your specific examples in C. Once again, relational operators in C always produce int(not bool) results regardless of the version of the language specification.

PS C 语言也有专用的布尔类型_Bool(宏别名为bool),其整数转换规则与 C++ 中基本相同。但是,这与您在 C 中的具体示例无关。再说一次,C 中的关系运算符总是产生int(not bool) 结果,而不管语言规范的版本如何。

回答by AnT

Section 6.5.8.6 of the C standard says:

C 标准的第 6.5.8.6 节说:

Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false.) The result has type int.

如果指定的关系为真,则每个运算符 <(小于)、>(大于)、<=(小于或等于)和 >=(大于或等于)应产生 1,如果是,则产生 0 false。)结果的类型为 int。

回答by Alex James

There seems to be no problem since the int to bool cast is done implicitly. This works in Microsoft Visual C++, GCC and Intel C++ compiler. No problem in either C or C++.

似乎没有问题,因为 int 到 bool 的转换是隐式完成的。这适用于 Microsoft Visual C++、GCC 和 Intel C++ 编译器。在 C 或 C++ 中都没有问题。