在 C/C++ 中将 int 转换为 bool [英] Casting int to bool in C/C++

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

问题描述

我知道在 C 和 C++ 中,当将 bool 转换为 int 时,(int)true == 1(int)false == 0.我想知道反向投射...

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

在下面的代码中,以下所有断言在使用 Visual Studio 2013 和 Keil µVision 5 编译的 .c 文件中对我来说都是正确的.注意 (bool)2 == true.

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.

C 和 C++ 标准对将非零、非 1 整数转换为 bool 有什么看法?是否指定了此行为?请包括引文.

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

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

  1. 反向转换(int --> bool).
  2. 没有讨论非零、非 1 的值.

推荐答案

0个基本类型(1)(2)映射到false.

其他值映射到 true.

这个约定是在原始 C 中建立的,通过其流控制语句;C 当时没有布尔类型.

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

假设作为函数返回值,false 表示失败是一个常见的错误.但特别是从 main 开始,false 表示成功.我已经多次看到这种情况做错了,包括在 D 语言的 Windows 入门代码中(当你有像 Walter Bright 和 Andrei Alexandrescu 这样的人弄错了,那么就很容易出错了),因此这个单挑要当心.

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.

对于内置类型,无需强制转换为 bool,因为该转换是隐式的.但是,Visual C++(Microsoft 的 C++ 编译器)倾向于为此发出性能警告 (!),这是一个纯粹的愚蠢警告.演员表不足以将其关闭,但通过双重否定的转换,即 return !!x,效果很好.可以将 !! 读作转换为 bool"运算符,很像 --> 可以读作去".对于那些深入了解运算符符号的可读性的人.;-)

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 “零值、空指针值或空成员指针值转换为false;任何其他值都将转换为 true.对于直接初始化 (8.5),std::nullptr_t 类型的纯右值可以转换为 bool 类型的纯右值;结果值为 false.”
2) C99 和 C11 §6.3.1.2/1 “当任何标量值转换为 _Bool 时,如果值比较等于 0,则结​​果为 0;否则,结果为 1."

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

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

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