在 C/C++ 中将 int 转换为 bool
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31551888/
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
Casting int to bool in C/C++
提问by cp.engr
I know that in C and C++, when casting bools to ints, (int)true == 1
and (int)false == 0
. I'm wondering about casting in the reverse direction...
我知道在 C 和 C++ 中,当将 bool 转换为 int(int)true == 1
和(int)false == 0
. 我想知道反向投射...
In the code below, all of the following assertions held true for me in .c files compiled with Visual Studio 2013 and Keil μVision 5. Notice (bool)2 == true
.
在下面的代码中,以下所有断言在使用 Visual Studio 2013 和 Keil μVision 5 编译的 .c 文件中对我来说都是正确的。注意(bool)2 == true
。
What do the C and C++ standards say about casting non-zero, non-one integers to bools? Is this behavior specified? Please include citations.
C 和 C++ 标准对将非零、非 1 整数转换为 bool 有什么看法?是否指定了此行为?请包括引文。
#include <stdbool.h>
#include <assert.h>
void TestBoolCast(void)
{
int i0 = 0, i1 = 1, i2 = 2;
assert((bool)i0 == false);
assert((bool)i1 == true);
assert((bool)i2 == true);
assert(!!i0 == false);
assert(!!i1 == true);
assert(!!i2 == true);
}
Nota duplicate of Can I assume (bool)true == (int)1 for any C++ compiler?:
不是我可以为任何 C++ 编译器假设 (bool)true == (int)1的副本吗?:
- Casting in the reverse direction (int --> bool).
- No discussion there of non-zero, non-one values.
- 反向转换(int --> bool)。
- 没有讨论非零、非 1 的值。
回答by Cheers and hth. - Alf
0 values of basic types (1)(2)map to false
.
基本类型(1)(2) 的0 值映射到false
.
Other values map to true
.
其他值映射到true
.
This convention was established in original C, via its flow control statements; C didn't have a boolean type at the time.
该约定是在原始 C 中通过其流控制语句建立的;C 当时没有布尔类型。
It's a common error to assume that as function return values, false
indicates failure. But in particular from main
it's false
that indicates success. I've seen this done wrong many times, including in the Windows starter code for the D language (when you have folks like Walter Bright and Andrei Alexandrescu getting it wrong, then it's just dang easyto get wrong), hence this heads-up beware beware.
假设作为函数返回值false
表示失败是一个常见的错误。但特别是从main
它false
表明成功。我已经多次看到这个错误,包括在 D 语言的 Windows 启动代码中(当你有像 Walter Bright 和 Andrei Alexandrescu 这样的人出错时,那么它很容易出错),因此这个单挑当心当心。
There's no need to cast to bool
for built-in types because that conversion is implicit. However, Visual C++ (Microsoft's C++ compiler) has a tendency to issue a performance warning (!) for this, a pure silly-warning. A cast doesn't suffice to shut it up, but a conversion via double negation, i.e. return !!x
, works nicely. One can read !!
as a “convert to bool
” operator, much as -->
can be read as “goes to”. For those who are deeply into readability of operator notation. ;-)
无需bool
为内置类型强制转换为,因为该转换是隐式的。但是,Visual C++(Microsoft 的 C++ 编译器)倾向于为此发出性能警告 (!),纯粹是愚蠢的警告。演员表不足以将其关闭,但通过双重否定的转换,即return !!x
,效果很好。可以读!!
作“转换为bool
”运算符,就像-->
可以读作“转到”一样。对于那些深入了解运算符符号可读性的人。;-)
1)C++14 §4.12/1 “A zero value, null pointer value, or null member pointer value is converted to false
; any other value is converted to true
. For direct-initialization (8.5), a prvalue of type std::nullptr_t
can be converted to a prvalue of type bool
; the resulting value is false
.”
2)C99 and C11 §6.3.1.2/1 “When any scalar value is converted to _Bool
, the result is 0 if the value compares equal to 0; otherwise, the result is 1.”
1)C++14 §4.12/1 “零值、空指针值或空成员指针值被转换为false
;任何其他值都转换为true
. 对于直接初始化(8.5),类型的纯右值std::nullptr_t
可以转换为类型的纯右值bool
;结果值为false
。”
2)C99 和 C11 §6.3.1.2/1 “当任何标量值转换为 时_Bool
,如果值比较等于 0,则结果为 0;否则,结果为 1。”
回答by too honest for this site
The following cites the C11 standard (final draft).
下面引用C11标准(最终草案)。
6.3.1.2:When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.
6.3.1.2:当任何标量值转换为_Bool时,如果值比较等于0,则结果为0;否则,结果为 1。
bool
(mapped by stdbool.h
to the internal name _Bool
for C) itself is an unsigned integer type:
bool
(映射stdbool.h
到_Bool
C的内部名称)本身是一个无符号整数类型:
... The type _Bool and the unsigned integer types that correspond to the standard signed integer types are the standard unsigned integer types.
... _Bool 类型和与标准有符号整数类型对应的无符号整数类型是标准无符号整数类型。
According to 6.2.5p2:
根据 6.2.5p2:
An object declared as type _Bool is large enough to store the values 0 and 1.
声明为 _Bool 类型的对象足够大,可以存储值 0 和 1。
AFAIK these definitions are semantically identical to C++ - with the minor difference of the built-in(!) names. bool
for C++ and _Bool
for C.
AFAIK 这些定义在语义上与 C++ 相同 - 内置(!)名称的细微差别。bool
对于 C++ 和_Bool
C。
Note that C does not use the term rvaluesas C++ does. However, in C pointers are scalars, so assigning a pointer to a _Bool
behaves as in C++.
请注意,C 不像C++ 那样使用术语右值。但是,在 C 中,指针是scalars,因此将指针分配给 a 的_Bool
行为与在 C++ 中一样。
回答by Mykyta Kozlov
There some kind of old school 'Marxismic' way to the cast int -> bool without C4800 warnings of Microsoft's cl compiler - is to use negation of negation.
有某种老派的“马克思主义”方式来转换 int -> bool 而没有微软的 cl 编译器的 C4800 警告 - 是使用否定的否定。
int i = 0;
bool bi = !!i;
int j = 1;
bool bj = !!j;