当类型转换为 int 时,C/C++ bool 类型是否总是保证为 0 或 1?

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

Is C/C++ bool type always guaranteed to be 0 or 1 when typecast'ed to int?

c++cboolean

提问by mojuba

Many compilers seem to be keeping only 0 or 1 in bool values, but I'm not sure this will always work:

许多编译器似乎只在 bool 值中保留 0 或 1,但我不确定这是否总是有效:

int a = 2;
bool b = a;
int c = 3 + b; // 4 or 5?

回答by Matthew Flaschen

Yes:

是的:

In C++ (§4.5/4):

在 C++(第 4.5/4 节)中:

An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one.

bool 类型的右值可以转换为 int 类型的右值,false 变为 0,true 变为 1。

In C, when a value is converted to _Bool, it becomes 0 or 1 (§6.3.1.2/1):

在 C 中,当一个值转换为 时_Bool,它会变成 0 或 1(第 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.

当任何标量值转换为_Bool 时,如果值比较等于0,则结果为0;否则,结果为 1。

When converting to int, it's pretty straight-forward. intcan hold 0 and 1, so there's no change in value (§6.3.1.3).

转换为 时int,它非常简单。 int可以容纳 0 和 1,因此值没有变化(第 6.3.1.3 节)。

回答by fredoverflow

Well, not always...

嗯,并不总是...

const int n = 100;
bool b[n];
for (int i = 0; i < n; ++i)
{
    int x = b[i];
    if (x & ~1)
    {
        std::cout << x << ' ';
    }
}

Output on my system:

我的系统上的输出:

28 255 34 148 92 192 119 46 165 192 119 232 26 195 119 44 255 34 96 157 192 119
8 47 78 192 119 41 78 192 119 8 250 64 2 194 205 146 124 192 73 64 4 255 34 56 2
55 34 224 255 34 148 92 192 119 80 40 190 119 255 255 255 255 41 78 192 119 66 7
8 192 119 192 73 64 240 255 34 25 74 64 192 73 64

The reason for this apparently weird output is laid out in the standard, 3.9.1 §6:

这种明显奇怪的输出的原因在标准 3.9.1 §6 中列出:

Values of type boolare either trueor false. Using a boolvalue in ways described by this International Standard as "undefined", such as by examining the value of an uninitialized automatic object, might cause it to behave as if it is neither truenor false.

typebool的值为truefalsebool以本国际标准描述为“未定义”的方式使用值,例如通过检查未初始化的自动对象的值,可能会导致它表现得好像它既不是true也不是false

回答by Prasoon Saurav

Is C/C++ .......

是 C/C++ ......

There's no language named C/C++.

没有名为 C/C++ 的语言。

bool type always guaranteed to be 0 or 1 when typecast'ed to int?

当类型转换为 int 时,bool 类型总是保证为 0 或 1?

In C++ yes because section $4.5/4 says

在 C++ 中是的,因为第 $4.5/4 节说

An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one.

bool 类型的右值可以转换为 int 类型的右值,false 变为 0,true 变为 1。

.

.

int c = 3 + b;// 4 or 5?

int c = 3 + b;// 4 还是 5?

The value of c will be 4

c 的值将是 4

回答by Alex Che

One more example when you are out of the safe boat:

当您离开安全船时,再举一个例子:

  bool b = false;
  *(reinterpret_cast<char*>(&b)) = 0xFF;
  int from_bool = b;
  cout << from_bool << " is " << (b ? "true" : "false");

Output (g++ (GCC) 4.4.7):

输出(g++(GCC)4.4.7):

  255 is true

To be added to the FredOverflow's example.

要添加到FredOverflow 的示例中

回答by hhafez

There is no bool type in C pre C99 (Such as C90), however the bool type in C99/C++ is always guaranteed to be 0 or 1.

C99 之前的 C 中没有 bool 类型(例如 C90),但是 C99/C++ 中的 bool 类型始终保证为 0 或 1。

In C, all boolean operation are guaranteed to return either 0 or 1, whether the bool type is defined or not.

在 C 中,无论是否定义了 bool 类型,所有布尔运算都保证返回 0 或 1。

So a && bor !aor a || bwill always return 0 or 1 in C or C++ regardless of the type of aand b.

So a && bor !aora || b在 C 或 C++ 中总是返回 0 或 1,而不管aand的类型如何b

回答by supercat

Types with padding bits may behave strangely if the padding bits don't hold the values expected for the type. Most C89 implementations didn't use padding bits with any of their integer types, but C99 requires that implementations define such a type: _Bool. Reading a _Boolwhen all of its bits are zero will yield zero. Writing any non-zero value to a _Boolwill set its bits to some pattern which will yield 1 when read. Writing zero will set the bits to a pattern (which may or may not be all-bits-zero) which will yield 0 when read.

如果填充位不包含该类型的预期值,则带有填充位的类型的行为可能会很奇怪。大多数 C89 实现不使用任何整数类型的填充位,但 C99 要求实现定义这样的类型:_Bool. _Bool当所有位为零时读取 a将产生零。将任何非零值写入 a_Bool会将其位设置为某种模式,读取时将产生 1。写入零会将位设置为一个模式(可能是也可能不是全位为零),读取时将产生 0。

Unless specified otherwise in an implementation's documentation, any bit pattern other than all-bits-zero which could not have been produced by storing a zero or non-zero value to a _Boolis a trap representation; the Standard says nothing about what will happen if an attempt is made to read such a value. Given, e.g.

除非在实现的文档中另有说明,否则通过将零或非零值存储到 a 无法产生的所有位为零以外的任何位模式_Bool都是陷阱表示;标准没有说明如果尝试读取这样的值会发生什么。给定,例如

union boolChar { _Bool b; unsigned char c; } bc;

storing zero to bc.cand reading bc.bwill yield zero. Storing zero or one to bc.bwill set bc.cto values which, if written, will cause bc.bto hold zero or one. Storing any other value to bc.cand reading bc.bwill yield Undefined Behavior.

将零存储到bc.c并读取bc.b将产生零。将零或一存储到bc.b将设置bc.c为如果写入将导致bc.b保持零或一的值。将任何其他值存储到bc.c并读取bc.b将产生未定义的行为。