views:

1014

answers:

7

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?

+8  A: 

Because !integerValue means integerValue == 0 and !!integerValue thus means integerValue != 0, a valid expression returning a bool. The latter is a cast with information loss.

StampedeXV
+5  A: 

After the double negation, the value is guaranteed to be 0 or 1; the original value might be any int value.

Martin v. Löwis
But why will the second statement not do exactly the same?
sharptooth
@sharptooth: See @John Scipione's answer: http://stackoverflow.com/questions/1310344/1310369#1310369
Hosam Aly
why aren't more people clueing into the fact that _this_ is the correct answer. double negative is very common in low level code
Matt Joiner
+23  A: 

Historically, the !! metaphor was used to ensure that your bool really contained one of the two values expected in a bool-like variable, because C and C++ didn't have a true bool type and we faked it with ints. This is less of an issue now with "real" bools.

But using !! is an efficient means of documenting (for both the compiler and any future people working in your code) that yes, you really did intend to cast that int to a bool.

T.J. Crowder
I wouldn't say "an efficient means of documenting" if the OP didn't even understand what it meant. A much better way would be static_cast<bool>(integerValue).
arolson101
The OP probably didn't know what `List<String>` meant at first, either, or `||=`. `!!` is a very, very deeply ingrained idiom in the C and C++ universe. If anyone doesn't know it, he/she should learn it -- and then make their own reasoned assessment about whether to use it.
T.J. Crowder
what's wrong with `bool var = (bool)intVar;` ?
bobobobo
@arolson,@T.J. Crowder: !! may be efficient, but it's ineffective
Lie Ryan
+5  A: 

A bool can only have two states, 0, and 1. An integer can have any state from -2147483648 to 2147483647 assuming a signed 32-bit integer. The unary ! operator outputs 1 if the input is 0 and outputs 0 if the input is anything except 0. So !0 = 1 and !234 = 0. The second ! simply switches the output so 0 becomes 1 and 1 becomes 0.

So the first statement guarantees that booleanValue will be be set equal to either 0 or 1 and no other value, the second statement does not.

John Scipione
This is completely wrong. The second statement involves an implicit `int`->`bool` cast. This is well-defined, and any non-zero `int` will be converted to `true`, and 0 will be converted to `false`. The warning that VC++ gives there is actually a "performance warning", and has nothing to do with this.
Pavel Minaev
I think you are confusing BOOL with bool. With bool, you get a real bool with an implicit cast, hence true or false.
Recep
The States of a bool in C++ are not 1 and 0, but true and false.
Johannes Schaub - litb
Thank you, I did not know that. I thought that 0 == false and 1 == true but apparently that is not so.
John Scipione
you were right the first time john scipione, this has it's place
Matt Joiner
+29  A: 

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.

+1 for "bool isNonZero = (integerValue != 0);". I always hate it when code conflates ints, doubles, ptrs, etc with bool's. They're different types and should be treat as such. The implicit cast of a bool to numeric type is an anchronism.
jon hanson
I don't see a problem with treating ints/pointers as bools - it's a stock C/C++ idiom, and any developer proficient in them should be expected to know it. However, sometimes you want to avoid compiler warnings and such - and in those cases an expicit comparison (or a cast) is definitely much preferable to the relatively obscure `!!` trick.
Pavel Minaev
Ints and bools are different. Why should 0 == false. It's a hangover from the days before the bool type. It also means that !!a doesn't necessarily equal a which is counter-intuitive.
jon hanson
0 is equal to false because the standard says so ;-) !! is a pretty common idiom you just seem to be not familiar with. That doesn_t make it neccessarily bad, except for the use at the beginning of a natural language sentence.
drhirsch
After over 20 years of C/C++ I'm well familiar with the idiom; i just think it is counter-intuitive!
jon hanson
+1 for "look how cute we can be with C/C++". Perfectly sums up so much of the bad coding practices that the C/C++ syntax and loose rules enable.
David
-1 for "bool isNonZero = (integerValue != 0);", this is neither more readable nor more "intuitive". If you don't like standard C/C++ idioms like !!, use a different language.
drhirsch
A: 

No big reason except being paranoid or yelling through code that its a bool.

for compiler in the end it wont make difference .

Learner
The code generated may well be "slower": the integer will require a test to see whether it is zero. However, for a boolean that's represented as 0 / 1 binary, no test is needed.
Johannes Schaub - litb
But, well, with this rational we should have a performance warning for every virtual function call...
gimpf
A: 

I've never like this technique of converting to a bool data type - it smells wrong!

Instead, we're using a handy template called boolean_cast found here. It's a flexible solution that's more explicit in what it's doing and can used as follows:

bool IsWindow = boolean_cast< bool >(::IsWindow(hWnd));
Alan
It's a bit of an overkill isn't it? I mean at the end of the day conversion to bool is a loss in precision and boils down to something like return b ? true : false;Which is about the same as !!TRUE Personally this is overengineered STL-like fetish.
Igor Zevaka
I take it you don't agree with using boolean_cast then (hence the down vote) - what would you suggest we use then?
Alan
I suggest you use the language as it was meant to be used. High performance, and nasty. Just cut the BS and get the job done.
Matt Joiner