Possible Duplicate:
Double Negation in C++ code.
I’m reading a code base, and find something like this:
#define uassert(msgid, msg, expr) (void)((!!(expr))||(uasserted(msgid, msg), 0))
I cannot figure out why (!!(expr)) is used instead of a single (expr). Anyway, a double negative means positive, is not it? Am I missing something?
It is a way to cast an expression to bool. In C++ though, operator! can be overloaded. Another way for C/C++:
C++ only way:
[Edit]
Thinking more about C++ operator overloading, it make sense to avoid using operators. Libraries like Boost.Spirit and Boost.Lambda use expression templates and lazy evaluation, so that expressions like
(expr) || call()may behave not as expected. The most bullet proof version of that macro looks like this:Here, only conversion of
exprto bool is used. Theelsebranch is needed to protect from expressions likeuassert(x) else something_else();.