Is using '!!' in C/C++ good practice and is it common?

c, c++

Solution

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.

Problem

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; ```

Original source

Related problems