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
bool to int conversion
提问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. bool
to int
conversion is implicit!
完全便携。符合标准。bool
以int
转换是隐式的!
§4.7/4 from the C++ Standard says (Integral Conversion)
C++ 标准中的 §4.7/4 说(积分转换)
If the source type is bool, the value
false
is converted to zeroand the valuetrue
is converted to one.
如果源类型为 bool ,则将值
false
转换为零,并将值true
转换为 1。
As for C, as far as I know there is no bool
in C. (before 1999) So bool
to int
conversion is relevant in C++ only. In C, 4<5
evaluates to int
value, in this case the value is 1
, 4>5
would evaluate to 0
.
至于C,据我所知是没有bool
的C.(1999年以前),所以bool
要int
转换在C ++中才有意义。在 C 中,4<5
计算为int
value,在这种情况下,值为1
, 4>5
将计算为0
。
EDIT: Jens in the comment said, C99 has _Bool
type. bool
is a macro defined in stdbool.h
header file. true
and false
are also macro defined in stdbool.h
.
编辑:Jens 在评论中说,C99 有_Bool
类型。bool
是stdbool.h
头文件中定义的宏。true
并且false
也是在stdbool.h
.
§7.16 from C99 says,
C99 中的 §7.16 说,
The macro
bool
expands to _Bool.[..]
true
which expands to the integer constant1
,false
which expands to the integer constant0
,[..]
宏
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 bool
whatsoever (that applies to C99 as well). In C language relational operators do not produce bool
results. Both 4 > 5
and 4 < 5
are expressions that produce results of type int
with values 0
or 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 > 5
并4 < 5
是产生类型的结果的表达式int
用值0
或1
。因此,在您的 C 示例中没有发生任何类型的“bool 到 int 转换”。
In C++ relational operators do indeed produce bool
results. bool
values are convertible to int
type, with true
converting to 1
and false
converting to 0
. This is guaranteed by the language.
在 C++ 中,关系运算符确实会产生bool
结果。bool
值可以转换为int
类型,true
转换为1
和false
转换为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++ 中都没有问题。