views:

91

answers:

3

I have just installed the Windows SDK v7.1 (MSVC 10.0) and running my code through (almost) full warning level (W3, default for qmake's CONFIG += warn_on) and am surprised about warning C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning)

In the following code, stream is an std::istream and token is a std::string.

// in some function returning bool
return (stream >> token) // triggers warning c4800:
                         // '(void *)': forcing value to bool 'true' or 'false' (performance warning)`
// somewhere else
if( stream >> token ) // does not trigger warning c4800

What is going on here? I don't even get why the warning is triggered in the first place. I thought the first bit of code already returned a bool anyways.

I understand this is nitpicking and the warning shouldn't even exist, but it's the only one in my code between MSVC's /W3 and gcc's -Wall -pedantic, so I'd like to know :)

SMALL UPDATE: I understand that the warning is designed to let you know you're assuming int->bool conversion, but 1) why would you even still use bool (==typedef int mostly) and 2) why would an if(2) not convert 2 to true or false, I thought that was the whole idea of a predicate, being true or false.

+2  A: 

The difference between the cases is probably that return needs to generate a return value, while if doesn't have to generate any value, but simply branch. The warning is apparently that a new data object would have to be created, so it's triggered by return but not by if.

David Thornley
A: 

If conditions are evaluated as zero or non-zero, not converted to Booleans.

i.e. if( 1 ) doesn't generate a warning, because 1 is non-zero. if( true ) doesn't generate a warning, because true is evaluated as non-zero.

So, integers, pointers, booleans are all converted to 'zero or non-zero', not to booleans.

But when you attempt to return an integer value from a function with a boolean return, it has to assume you want to mimic that behaviour. That's why it indicates "I'm assuming you want to cast 'non-zero to true and zero to false', and you get the warning.

Shawn D.
This is incorrect. §6.4/4 states that (except for a `switch`) a condition is converted to a bool. And then an `if` (for example) has its statement executed if that value is `true`.
GMan
@GMan: exactly what I was thinking. Check question update
rubenvb
@Gman: The standard states the behavior, not the exact code that the compiler must follow. Converting to a bool and then branching depending on the value (in many architectures an integer type), is semantically equivalent to not converting and branching on zero/non-zero. The standard in §1.9/1 commented in footnote 5) *This provision is sometimes called the “as-if” rule, because an implementation is free to disregard any requirement of this Intl. Standard as long as the result is as if the requirement had been obeyed, as far as can be determined from the observable behavior of the program.*
David Rodríguez - dribeas
@David: Right, but the answer says they are definitely never treated as booleans (and instead as 0 or non-0), which is totally false. They are always treated as booleans. (It's just that the conversion may be done by false being 0 and true being non-0. But the condition itself *is* a boolean value.)
GMan
+4  A: 

What is going on here? I don't even get why the warning is triggered in the first place. I thought the first bit of code already returned a bool anyways.

  1. Streams have an implicit conversion operator that returns a void*. (That's a version of the safe bool idiom. It's done this way because there is less contexts in which void* compiles than bool, so there's less contexts in which the implicit conversion could kick in unwanted.)[1]

  2. Streams' operator>>() returns a reference to its left operand - the stream. That's so you can chain input operations: strm >> value1 >> value2 is executed as ((strm >> value1) >> value2).

Now, when you say if( strm >> value ), strm >> value is executed and a stream returned. In order to put that into an if statement, the implicit conversion into void* is performed, and that pointer is then checked for being NULL or not.
That's no different from if(ptr), the if statement implicit converts its condition to bool, but compilers would never warn about that, because the condition not being a bool is so common.

With the return, this is different. If you want to return a certain type, usually the expression you return should be of that type. VC's warning is quite annoying and for me in 99 out of 100 times it was superfluous. But the remaining 1% (never a performance concern, BTW; I think that is silly about the warning) made me glad the warning is there.

The workaround for this warning is to

return 0 != <expression>

where <expression> is whatever you think should be considered a boolean value.

[1] ISTR Stroustrup writing somewhere that an operator bool() would silently compile if you messed up the operators: ostrm >> 5; (note the >> instead of <<) would compile fine, but silently do the wrong thing. (It converts the boolean to an integer and right-shifts that 5 times, then discard the value.)

sbi
So basically the compiler is saying: "Hey, the type of your return expression doesn't match your return type, make it explicit to be sure."?
GMan
Thanks for the full explanation, but it still suggests msvc is not standards compliant for an `if`, which should convert the condition to a `bool`
rubenvb
@ruben: Warnings have nothing to do with compliance. The condition for the `if` is certainly converted to a `bool`, but the warning isn't strictly about conversion, it's about converting to generate a "real" value. (As opposed to a "temporary" one.)
GMan
+1 But to be picky, I believe its a void * conversion, not const void *. See 27.4.4.
anon
@GMan: well, I'm sorting this warning under: "incredibly unsensible for the most part" ;) Thanks guys.
rubenvb
@Neil: You're right, thanks. `<sigh>` [And I had just been at it today!](http://stackoverflow.com/questions/3222131)
sbi
@rubenvb: I've tried to change the wording around the `if(ptr)`, so that it is less ambiguous regarding the implicit conversion to `bool`. Sorry if I caused any confusion.
sbi
The condition does not need to be converted to a bool in the if case. The compiler must provide code that is semantically equivalent to what you have written, but in many cases the processor has instructions that can jump on zero/non-zero values for both integer registers and pointers, that means that `if (p)` can be directly translated to a native jump instruction. On the other hand, if you are returning that value, the expression must be evaluated and converted to the return type, forcing the actual conversion.
David Rodríguez - dribeas
@David: That's an implementation detail. Your comparison is false, an `if` must also have "the expression be evaluated and converted to [a boolean], forcing the actual conversion." §4.2 and §6.4/4 both state this.
GMan
I added a similar comment to your answer, basically §1.9/1 which is referred to as the *as-if* rule says that the standard can disregarded as long as the behavior is equivalent. `if (p)` with `p` being an integer or pointer type is equivalent to `if (p!=0)`, and that is equivalent to loading `p` in a register and branching if the value is not zero to skip the block. When the standard states that the result is converted to bool, it is effectively saying that the code must behave as if the conversion was made.
David Rodríguez - dribeas
On the other hand, when you are returning a `bool` (and depending on the call convention) the compiler might have to convert the expression value to whatever representation a `bool` has for the compiler and write it to wherever it is needed. (And I have just realized that the comment is to @sdeer's answer and not yours, sorry for the confusion)
David Rodríguez - dribeas
@GMan: Sorry, I somehow missed the first comment by you the first round. Note that the warning message ends with "...(performance warning)". What that would be useful for is one of the questions I always wanted to know, but never got around asking.
sbi