In the following code, what is the benefit of using (!!p)
instead of (p != NULL)
?
AClass *p = getInstanceOfAClass();
if( !!p )
// do something
else
// do something without having valid pointer
In the following code, what is the benefit of using (!!p)
instead of (p != NULL)
?
AClass *p = getInstanceOfAClass();
if( !!p )
// do something
else
// do something without having valid pointer
That's a matter of style, in fact they are equivalent. See this very similar question for discussion.
IMO comparing against null pointer is clearer.
As far as I can see, it's just a shorter way to convert it into a boolean value. It applies the ! twice, though, whereas p != NULL
does one comparison. So I guess the benefit is just shorter code, albeit more cryptic if you don't know what !!p
is supposed to mean.
It is pretty much the same, although I consider the !!p
to be bad style, and usually indicates a coder trying to be clever.
They are the same, but I recommend to use
NULL != p
It is more readable.
Do !!NOT use double negation. A simple argument is that since C++ is a limited English subset and english just does not have a double negation then english speakers will have a lot of difficulty to parse what is going on.
There is no difference in the given example.
However the assumption that this applies to all cases is incorrect. a = not not b is not the same as a = b, as far as integer types are concerned.
In C, 0
is false. Anything but 0
is true. But not 0
is 1
, and nothing else. In C++, true casts to 1
as an integer, not only for backward compatibilty with C, but because 1
is not 0
, and 1
is the most common value used to denote true in C bool types, including the official C bool type, and BOOL
used in Win32.
While for the example code given, !!p
is unnecessary because the result is cast to a bool
for evaluation of the if
condition, that doesn't rule out the use of !!
for purposes of casting booleans to expected integer values. Personally in this example, to maximize the probability that type changes and semantics are clear, I would use NULL != p
or p != NULL
to make it absolutely clear what is meant.
This technique is known as the double-bang idiom, and this guy provides some good justifications.
I thing GMan’s original comment should be the accepted answer:
I wonder what's wrong with just
if (p)
The point is: nothing is wrong with it, and this should be the preferred way. First off, !!p
is “too clever”; it’s also completely unnecessary and thus bad (notice: we’re talking about pointers in an if
statement here, so Anacrolix’ comment, while generally valid, doesn’t apply here!).
The same goes for p != NULL
. While this is possible, it’s just not needed. It’s more code, it’s completely redundant code and hence it makes the code worse. The truest thing Jeff Atwood ever said was that “the best code is no code at all.” Avoid redundant syntax. Stick to the minimum (that still conveys the complete meaning; if (p)
is complete).
Finally, if (p)
is arguably the most idiomatic way to write this in C++. C++ bends over backwards to get this same behaviour for other types in the language (e.g. data streams), at the cost of some very weird quirks. The next version of the standard even introduces new a syntax to achieve this behaviour in user-defined types.
For pointers, we get the same for free. So use it.
/EDIT: About clarity: sharptooth writes that
IMO comparing against null pointer is clearer.
I claim that this is objectively wrong: if (p)
is clearer. There is no possible way that this statement could mean anything else, neither in this context nor in any other, in C++.