sharptooth sharptooth - 12 days ago 6
C++ Question

Why use !! when converting int to bool?

What can be a reason for converting an integer to a boolean in this way?

bool booleanValue = !!integerValue;


instead of just

bool booleanValue = integerValue;


All I know is that in VC++7 the latter will cause C4800 warning and the former will not. Is there any other difference between the two?

Answer

The problems with the "!!" idiom are that it's terse, hard to see, easy to mistake for a typo, easy to drop one of the "!'s", and so forth. I put it in the "look how cute we can be with C/C++" category.

Just write bool isNonZero = (integerValue != 0); ... be clear.