简体   繁体   中英

Magic of casting value to bool

Today I realized that casting a value to a bool is a kind of magic:

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 . I know that uc will contain 0x00, because only the LSB is copied. But b will be true , so my assumption is that, when casting to bool , the value gets evaluated instead of copied.

Is this assumption correct? And is this a standard C++ behavior?

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. It can be implemented as copying the LSB, but you should still think about it in the semantic sense, not the implementation.

On the same vein, conversion of int to bool is defined as value != 0 , so again, that is what you get.

Integral (and boolean) conversions are covered by the C++11 standard in [conv.integral] and [conv.bool] . For the C-style cast, see [expr.cast] and [expr.static.cast] .

Yes, when casting to bool , the value gets evaluated, not copying.

In fact, in your example, as long as value is not 0 , b will be true .

Update: Quoting from C++ Primer 5th Edition Chapter 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]

1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.

According to the rules for C-style casts , (bool)value is effectively a 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); . That's well-defined: Temp is true iff value != 0 . So yes, value is "evaluated" in a sense.

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:

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 .

Of course, C++ is backwards compatible with C, so it adopted the feature. C++ also added the ability to overload the conversion to bool on your classes. iostream does this to indicate when the stream is in an invalid state:

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

ISO/IEC C++ Standard:

4.12 Boolean conversions [conv.bool] 1 A prvalue of arithmetic... type can be converted to a prvalue of type bool. A zero value... is converted to false; any other value is converted to true.

So, since a prvalue is a value, you might say that the value gets evaluated, albeit that's kind of pleonastic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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