I often see experienced programmers write !!x
, even though the expected expression is a Boolean (i.e., zero or not zero) and not an integer.
For example, a line from boost:
BOOST_ASSERT(!!p); // where `p` is a pointer
What's the point of !!p
when just p
will do?
What I understand by a Boolean parameter is an expression converted to a value of integral type, and the value is compared against zero, explicitly or implicitly (with if
or its ternary operator equivalent).
Thus, anything that takes a Boolean and expects only 0
or 1
is wrongly implemented, if my understanding of Boolean is correct.
For clarification: it's obvious that !
converts to bool
; the question is explicitly asking for why.