consider following code compiled visual studio 2015:
#include <iostream> #include <cassert> void foo( bool b ) { std::cout << b; } int main() { int a; foo( = 2 ); // getting warning #4800 foo( !(a = 2) ); // not getting warning return 0; }
foo( = 2 )
produces warning 4800 'int': forcing value bool 'true' or 'false'
, fine.
but foo( !(a = 2) )
not produce warning. why? @ point there has been int bool cast!
foo(a = 2)
equivalent bool b = (a = 2)
. expression a = 2
returns a
, equivalent to
a = 2; bool b = a; //conversion of 'int' 'bool' -> warning!
foo(!(a = 2))
equivalent bool b = !(a = 2)
. expression a = 2
returns a
:
a = 2; bool b = !a; //'!a' legal => returns bool -> no warning!
note can apply operator!
int
, negates int
, , returns bool
. that's why there no performance warning.
Comments
Post a Comment