简体   繁体   English

铸造价值的魔力布尔

[英]Magic of casting value to bool

Today I realized that casting a value to a bool is a kind of magic: 今天我意识到为bool施加价值是一种魔力:

int value = 0x100;
unsigned char uc = static_cast<unsigned char>(value);
bool b = static_cast<bool>(value);

Both sizeof(uc) and sizeof(b) return 1 . sizeof(uc)sizeof(b)返回1 I know that uc will contain 0x00, because only the LSB is copied. 我知道uc将包含0x00,因为只复制了LSB。 But b will be true , so my assumption is that, when casting to bool , the value gets evaluated instead of copied. 但是b将是true ,所以我的假设是,当转换为bool ,值被评估而不是复制。

Is this assumption correct? 这个假设是否正确? And is this a standard C++ behavior? 这是标准的C ++行为吗?

There's nothing magical about it. 这没有什么神奇之处。 The conversion from int to unsigned char is defined as value % 256 (for 8-bit char s), so that is what you get. intunsigned char的转换定义为value % 256 (对于8位char ),这就是你得到的。 It can be implemented as copying the LSB, but you should still think about it in the semantic sense, not the implementation. 它可以实现为复制LSB,但你仍然应该在语义意义上考虑它,而不是实现。

On the same vein, conversion of int to bool is defined as value != 0 , so again, that is what you get. 同样,将int转换为bool定义为value != 0 ,所以再次,这就是你得到的。

Integral (and boolean) conversions are covered by the C++11 standard in [conv.integral] and [conv.bool] . [conv.integral][conv.bool]的C ++ 11标准涵盖了积分(和布尔)转换。 For the C-style cast, see [expr.cast] and [expr.static.cast] . 对于C风格的演员表,请参阅[expr.cast][expr.static.cast]

Yes, when casting to bool , the value gets evaluated, not copying. 是的,当转换为bool ,将评估值,而不是复制。

In fact, in your example, as long as value is not 0 , b will be true . 实际上,在您的示例中,只要value不为0b就为true

Update: Quoting from C++ Primer 5th Edition Chapter 2.1.2: 更新:从C ++ Primer第5版第2.1.2章引用:

When we assign one of the nonbool arithmetic types to a bool object, the result is false if the value is 0 and true otherwise.

It is a part of the standard: 它是标准的一部分:

4.12 Boolean conversions [conv.bool] 4.12布尔转换[conv.bool]

1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. 1算术,无范围枚举,指针或指向成员类型的指针的prvalue可以转换为bool类型的prvalue。 A zero value, null pointer value, or null member pointer value is converted to false; 零值,空指针值或空成员指针值转换为false; any other value is converted to true. 任何其他值都转换为true。 A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; 类型为std :: nullptr_t的prvalue可以转换为bool类型的prvalue; the resulting value is false. 结果值为false。

According to the rules for C-style casts , (bool)value is effectively a static_cast . 根据C风格的强制转换规则(bool)value实际上是static_cast The first rule for static_cast then kicks in, which computes the value of a temporary "declared and initialized ... as by new_type Temp(expression); ", ie bool Temp(value); static_cast的第一个规则然后开始,它计算临时“声明和初始化的值......如new_type Temp(expression); ”,即bool Temp(value); . That's well-defined: Temp is true iff value != 0 . 这是明确定义的:如果value != 0 Temptrue value != 0 So yes, value is "evaluated" in a sense. 所以是的, value在某种意义上是“评估”的。

Casting to bool is a feature inherited from plain old C. Originally, C didn't have a bool type, and it was useful to use other types in if statements, like so: 转换为bool是一个继承自普通旧C的特性。最初,C没有bool类型,在if语句中使用其他类型很有用,如下所示:

int myBool = 1;
if(myBool) {
    // C didn't have bools, so we had to use ints!
}

void* p = malloc(sizeof(int));
if(!p) {
    // malloc failed to allocate memory!
}

When you're converting to bool , it acts just like you're putting the statement in an if . 当你转换为bool ,它的行为就像你将语句放在if

Of course, C++ is backwards compatible with C, so it adopted the feature. 当然,C ++向后兼容C,所以它采用了这个功能。 C++ also added the ability to overload the conversion to bool on your classes. C ++还添加了将转换重载为类的bool功能。 iostream does this to indicate when the stream is in an invalid state: iostream执行此操作以指示流何时处于无效状态:

if(!cout) {
    // Something went horribly wrong with the standard output stream!
}

ISO/IEC C++ Standard: ISO / IEC C ++标准:

4.12 Boolean conversions [conv.bool] 1 A prvalue of arithmetic... type can be converted to a prvalue of type bool. 4.12布尔转换[conv.bool] 1算术...类型的prvalue可以转换为bool类型的prvalue。 A zero value... is converted to false; 零值...转换为false; any other value is converted to true. 任何其他值都转换为true。

So, since a prvalue is a value, you might say that the value gets evaluated, albeit that's kind of pleonastic. 因此,由于prvalue是一个值,你可能会说该值得到了评估,尽管这是一种多样性。

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

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