简体   繁体   English

C ++将布尔值转换为int-标准

[英]C++ casting bool to int - standard

I am interrested wheather standard says anything about possible values of bool type type after casting it to integer type. 我被惠特尔标准困扰,在将其转换为integer类型后,对bool类型的可能值作了任何说明。

For example following code: 例如下面的代码:

#include <iostream>
using namespace std;

int main() {
    bool someValue=false;
    *((int*)(&someValue)) = 50;

    cout << someValue << endl;

    return 0;
}

prints 1 even though it's forced to store value 50. Does standard specify anything about it? 即使强制存储值50也会打印1。standard是否指定了任何内容? Or is compiler generating some method for type bool as: 还是编译器为布尔类型生成某种方法,如:

operator int(){
    return myValue !=0 ? 1 : 0;
}

Also why is casting like following: 同样为什么要进行如下铸造:

reinterpret_cast<int>(someValue) = 50;

forbidden with error 错误禁止

error: invalid cast from type 'bool' to type 'int' 错误:从类型'bool'强制转换为类型'int'无效

(For all above I user GCC 5.1 compiler.) (对于所有上述用户,我使用GCC 5.1编译器。)

The way you are using it exhibits UB, because you write outside of the bool variable's boundaries AND you break strict aliasing rule. 使用它的方式显示为UB,因为您在bool变量的边界之外编写并且违反了严格的别名规则。

However, if you have a bool and want to use it as a an int (this usually happens when you want to index into an array based on some condition), the standard mandates that a true bool converts into 1 and false bool converts into 0 , no matter what (UB obviously excluded). 但是,如果您有一个布尔变量并想将其用作一个int值(通常会在您希望基于某种条件索引到数组中时发生这种情况),则标准要求将true bool转换为1 ,将false bool转换为0 ,无论如何(UB显然不包括在内)。

For example, this is guaranteed to output 52 as long as should_add == true . 例如,只要should_add == true保证输出52

int main(){
    int arr[] = {0, 10};
    bool should_add = 123;
    int result = 42 + arr[should_add];
    std::cout << result << '\n';
}

This line *((int*)(&someValue)) = 50; 这行*((int*)(&someValue)) = 50; is at least non standard. 至少是非标准的。 The implementation could use a lesser rank for bool (say 1 or 2 bytes) that for int (say 4 bytes). 对于布尔值(例如1或2个字节),该实现可以使用比整数值(例如4个字节)小的排序。 In that case, you would write past the variable possibly erasing an other variable. 在这种情况下,您将在变量后写,可能会擦除另一个变量。

And anyway, as you were said in comment, thanks to the strict aliasing rule almost any access through a casted pointer can be seen as Undefined Behaviour by a compiler. 而且,正如您在评论中所说,由于严格的别名规则,几乎所有通过强制转换指针的访问都可以被编译器视为未定义行为。 The only almost legal one (for the strict aliasing rule) would be: (对于严格的别名规则)唯一合法的是:

*((char *) &someValue) = 50; 

on a little endian system, and 在小端系统上,以及

*(((char *) &someValue) + sizeof(bool) - 1) = 50; 

on a big endian one (byte access has still not be forbidden). 在大端字节序上(仍不禁止字节访问)。

Anyway, as the representation of bool is not specified by the standard directly writing something in a bool can lead to true or false depending on implementation. 无论如何,由于标准没有指定布尔值的表示方式,因此根据实现的不同,直接在布尔值中写入内容可能会导致结果为真或为假。 For example an implementation could considere only the lowest level bit (true if val&1 is 1, else 0), another one could considere all bits (true for any non 0 value, false for only 0). 例如,一种实现可以仅考虑最低位(如果val&1为1,则为true,否则为0),另一种可以考虑所有位(对于任何非0值,为true;对于仅0,则为false)。 The only thing that standard says is that a conversion of a 0 leads to false and of a non 0 leads to true. 标准唯一说的是,0的转换将导致错误,非0的转换将导致true。


But was is mandated by standard is the conversion from bool to int: 但标准规定的是从bool到int的转换:

4.5 Integral promotions [conv.prom] 4.5整体促销[conv.prom]

... ...
A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one. bool类型的prvalue可以转换为int类型的prvalue,false变为零,true变为1。

So this fully explains that displaying a bool can only give 0 or 1 - even if as the previous operation invoked UB, anything could have happen here including this display 因此,这充分说明了显示布尔值只能给出0或1-即使在前一个操作调用UB的情况下,包括该显示在内的任何事情都可能在这里发生

You invoked Undefined Behaviour - shame on you

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM