Reputation: 31539
Lets say I have a pointer
MyType *ptr;
When checking the validity of that pointer in a "truth-value context" by the old standards I would write something like this
if (ptr) { ... // 1
while (ptr) { ... // 2
The thing is that in such "truth value contexes" we expect for the implicit conversion of a pointer to a boolean value to take place, so we would be pretty much be comparing
if (NULL != ptr) { ...
while (NULL != ptr) { ...
Yet comparing against a macro for the integer 0
is deprecated and C++11 proposes comparing against nullptr
.
When in a truth value context though like (1) or (2) above where we don't explicitly say
if (nullptr != ptr) { ...
while (nullptr != ptr) { ...
what is our pointer compared against ? It's conversion to a boolean ? Do we have to explicitly compare against nullptr
?
Upvotes: 2
Views: 1560
Reputation: 39121
The condition (if it's an expression) of an if
statement is contextually converted to bool
:
[stmt.select]/4 about the condition in selection statements (if
, switch
):
The value of a condition that is an expression is the value of the expression, contextually converted to
bool
for statements other thanswitch
; if that conversion is ill-formed, the program is ill-formed.
Contextual conversion to bool
is defined as follows in [conv]/3:
An expression
e
can be implicitly converted to a typeT
if and only if the declarationT t=e;
is well-formed, for some invented temporary variablet
. Certain language constructs require that an expression be converted to a Boolean value. An expressione
appearing in such a context is said to be contextually converted tobool
and is well-formed if and only if the declarationbool t(e);
is well-formed, for some invented temporary variablet
.
Here's the description of a conversion to bool
for fundamental types [conv.bool]/1:
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type
bool
. A zero value, null pointer value, or null member pointer value is converted tofalse
; any other value is converted totrue
. A prvalue of typestd::nullptr_t
can be converted to a prvalue of typebool
; the resulting value isfalse
.
So when we test a pointer if(ptr)
, we compare ptr
to the null pointer value of that type. What's a null pointer value? [conv.ptr]/1
A null pointer constant is an integral constant expression prvalue of integer type that evaluates to zero or a prvalue of type
std::nullptr_t
. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. Such a conversion is called a null pointer conversion. Two null pointer values of the same type shall compare equal.
This also describes what happens when we compare if(ptr != nullptr)
: The nullptr
is converted to the type of ptr
(see [expr.rel]/2), and yields the null pointer value of that type. Hence, the comparison is equivalent to if(ptr)
.
Upvotes: 5
Reputation: 206647
Let's say you have:
int* ip = foo();
if ( nullptr == ip )
{
}
It's as if you are saying:
int* ip = foo();
if ( (int*)0 == ip )
{
}
At that point, you are comparing two pointers of the same type.
This is what I found at cppreference.com
Explanation
The keyword nullptr denotes the null pointer literal. It is an unspecified prvalue of type std::nullptr_t. There exist implicit conversions from nullptr to null pointer value of any pointer type and any pointer to member type. Similar conversions exist for any value of type std::nullptr_t as well as for the macro NULL, the null pointer constant.
Upvotes: 1