For most pedantic answers, the relevant sections of the specification are given here.
First, here is how the if , from & sect; 6.4.4:
The condition value, which is the initialized declaration in a statement other than the switch statement, is the value of the declared variable implicitly converted to type bool . If this transformation is poorly formed, the program is poorly formed.
"But how do pointers to bool s translate?" you can ask. So here & sect; 4.12.1 :: -)
The value of arithmetic, enumeration, pointer or a pointer to a member type can be converted to an rvalue of type bool. A null value, a null pointer value, or a null element pointer value is converted to false; any other value is converted to true.
So that means statement
if (ptr)
equivalently
if ((bool) ptr)
which in turn is equivalent
if (ptr == NULL)
But what about
if (!ptr)
Well, the C ++ specification, section 5.3.1.8, says that
The operand of the logical negation operator! implicitly converted to bool (clause 4); its value is true if the converted operand is false and false otherwise. The result type is bool .
So that means that
if (!ptr)
equivalently
if (!(bool)ptr)
which in turn is equivalent
if (!(ptr == NULL))
which is finally equivalent
if (ptr != NULL)
Phew! It was an interesting search. Hope this answers your question!
Of course, this story has more. NULL not part of C ++; this is a macro in <cstddef> defined as
#define NULL 0
This works because the C ++ standard defines a null pointer in & sect; 4.10.1 how
The null pointer constant is the integral constant expression (5.19) of the rvalue type of an integer type that evaluates to zero . The null pointer constant can be converted to a pointer type; the result is a null pointer value of this type and is different from any other value of a pointer to an object or a pointer to a function type
So, to be more correct, I had to use the numeric literal 0 in the above examples. However, if you included <cstddef> , then after preprocessing this will work with the same code.