铸造int在C / C ++ bool的 [英] Casting int to bool in C/C++

查看:142
本文介绍了铸造int在C / C ++ bool的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,在C和C ++,铸造布尔变量为整数时,(INT)真== 1 (INT)假== 0 。我想了解一下在相反的方向铸造...

在下面的code,以下所有断言在与Visual Studio 2013和Keil的μVision5.注意(布尔)2 ==真

什么是C和C ++标准说的铸造不为零,无一整数的bool?这种行为规定?请包括引用。

 的#include< stdbool.h>
#包括LT&;&ASSERT.H GT;无效TestBoolCast(无效)
{
    INT I0 = 0,I1 = 1,I2 = 2;    断言((布尔)10 ==假);
    断言((布尔)I1 == TRUE);
    断言((布尔)12 ==真);    断言(!! I0 ==假);
    断言(!! I1 == TRUE);
    断言(!! I2 == TRUE);
}

不可以 的副本我可以假设(布尔)真==(INT)1任何C ++编译器?


  1. 铸造在相反的方向(INT - >布尔)

  2. 的非零不讨论有,无一值。


解决方案
基本类型的

0值(1)(2)地图

其他值映射到真正

本公约成立于原来的C,通过其流控制语句; ç没有在当时是一个布尔类型。


这是一个常见的​​错误的假设,因为函数返回值,表示失败。但是,特别是从,表示成功。我已经看到了这个做错了很多次,包括在Windows启动code为D语言(当你有一个像沃尔特·布莱特和安德烈Alexandrescu的人得到它错了,那么它只是宕的容易的得到错误的),因此这单挑提防提防。


有没有必要转换为布尔用于内建类型,因为转换隐式的。但是,Visual C ++(微软的C ++编译器)已发出性能警告(!),对于这一点,一个纯粹的愚蠢预警的倾向。强制转换并不足以关起来,而是通过双重否定,即返回!X A转换,很好地工作。人们可以读 !! 作为“转换为布尔”的运营商,就像 - > 可作为&ldquo阅读,去&rdquo ;.对于那些谁是深入到运营商记号可读性。 ; - )



1) C ++ 14§4.12/ 1“零值,空指针值或空成员指针值转换为;任何其他的值转换为真正。对于直接初始化(8.5),键入的std :: nullptr_t 可以被转换成键入的prvalue布尔;结果值”的。结果
2) C99和C11§6.3.1.2/ 1“当任何标值转换为 _Bool ,结果是0,如果值进行比较等于0;否则,结果为1。”的

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...

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.

What do the C and C++ standards say about casting non-zero, non-one integers to bools? Is this behavior specified? Please include citations.

#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);
}

Not a duplicate of Can I assume (bool)true == (int)1 for any C++ compiler?:

  1. Casting in the reverse direction (int --> bool).
  2. No discussion there of non-zero, non-one values.

解决方案

0 values of basic types (1)(2)map to false.

Other values map to true.

This convention was established in original C, via its flow control statements; C didn't have a boolean type at the time.


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 easy to get wrong), hence this heads-up beware beware.


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. ;-)


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.”

这篇关于铸造int在C / C ++ bool的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆