Visual C ++ Warning C4800, why does it only start with return statements?

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

The following code streamis std::istream, but token- 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's going on here? I donโ€™t even understand why the warning is triggered in the first place. I thought the first bit of code already returned bool.

I understand that this is nitpicking and the warning should not exist, but it is the only one in my code between MSVC /W3and gcc -Wall -pedantic, so I would like to know :)

SMALL UPDATE: I understand that the warning is designed so that you know that you accept int-> bool conversion, but 1) why you will still use bool (== typedef intbasically) and 2) why if(2)not convert 2to true or false, I thought it was the whole predicate idea that is true or false.

+3
source share
3 answers

What's going on here? I donโ€™t even understand why the warning is triggered in the first place. I thought the first bit of code already returned bool.

  • , void*. ( bool. , , void* , bool, , .) [1]

  • operator>>() - . , : strm >> value1 >> value2 ((strm >> value1) >> value2).

, if( strm >> value ), strm >> value . if, void*, NULL .
if(ptr), if bool, , , bool, .

return . , , , . VC , 99 100 . 1% ( , BTW, , ) .

-

return 0 != <expression>

<expression> - , , .

[1] ISTR Stroustrup -, operator bool() , : ostrm >> 5; ( , >> <<) , , ( , 5 , .)

+5

, , , return , if - , . -, , , return, if.

+3

, Booleans.

. (1) , 1 . if (true) , true .

, , , " ", booleans.

, , . : " , " "" "" "), .

0
source

Source: https://habr.com/ru/post/1754290/


All Articles