Question

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 ?

Was it helpful?

Solution

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 than switch; 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 type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t. Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t.

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 to false; any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.

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).

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top