Re: When int meets bool over ==
* Alf P. Steinbach:
[at least two typos, as follows]
[snip]
Then there is the operator precedence question.
Consider e.g.
if( a && b == true ) { ... }
which in C++ has very different effect from
if( a && b ) { ... }
For "different effect" the concrete example should have been
if( a && b == false ) { ... }
versus
if( !(a && b) ) { ... }
With 'a' false the former expression yields false,
false && (b == false) -> false
while the latter expression yields true:
!(false && b) -> !false -> true
The advice & general reasoning was correct, but the concrete example
was meaningless.
[snip]
A naming scheme that takes this into account:
template< typename T >
bool failed( T const& v ) { return !v; }
template< typename T >
bool succeeded( T const& v ) { return !!v; }
...
if( failed( f() ) ) { throw std::runtime_error( "f failed" ); }
In the context I wrote this the called function would signal failure
by logical false result. Hence the 'failed' function should have
double (or no) negation and the 'succeeded' function single negation.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]