Possible Duplicate:
Double Negation in C++ code
As far as I know, no C/C++ books tutorials or manuals mention this technique. Maybe because it's just a tiny little thing, not worth mentioning.
I use it because C/C++ mixes bool type with int, long, pointer, double etc...together. It's very common to need to convert a non-bool to bool. It's not safe to use (bool)value to do that, so I use !!
to do it.
Example:
bool bValue = !!otherValue;
It's fine, any C or C++ programmer should recognize it, but I would prefer something more explicit like
(x != 0)
I think !!
is quite clear in comparison to some of the other choices such as :
if (foo)
bar = 1;
else
bar = 0;
or bar = foo ? 1 : 0;
Since !!
does exactly one thing, I find it very unambiguous.
In this exact case:
bool bValue = !!otherValue;
you don't need to write !!
. It will work fine without them:
bool bValue = otherValue;
I think in most cases implicit casting will be nice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With