Is !! a safe way to convert to bool in C++?

boolean, c++, visual-c++

Solution

The argument of the ! operator and the first argument of the ternary operator are both implicitly converted to bool, so !! and ?: are IMO silly redundant decorations of the cast. I vote for

b = (t != 0);

No implicit conversions.

Problem

[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`.

Original source

Related problems