[This question is related to but not the same as this one.]
If I try to use values of certain types as boolean expressions, I get a warning. Rather than suppress the warning, I sometimes use the ternary operator (?:
) to convert to a bool. Using two not operators (!!
) seems to do the same thing.
Here's what I mean:
typedef long T; // similar warning with void * or double
T t = 0;
bool b = t; // performance warning: forcing 'long' value to 'bool'
b = t ? true : false; // ok
b = !!t; // any different?
So, does the double-not technique really do the same thing? Is it any more or less safe than the ternary technique? Is this technique equally safe with non-integral types (e.g., with void *
or double
for T
)?
I'm not asking if !!t
is good style. I am asking if it is semantically different than t ? true : false
.
!!
on subjective basis. double negation is the idiomatic cast-to-bool vehicle in JavaScript, and if javascripters get it, I'd expect C++ users would too. – just somebody