Skip to main content

C++ named requirements: NullablePointer (since C++11)

Specifies that the type is a pointer-like object which can be compared to std::nullptr_t objects.

Requirements

The type must meet all of the following requirements:

In addition, a value-initialized object of the type must produce a null value of that type. This null value shall only be equivalent to itself. Default initialization of the type may have an indeterminate value.

An object of the type must be contextually convertible to bool. The effect of this conversion returns false if the value is equivalent to its null value and true otherwise.

None of the operations that this type performs may throw exceptions.

The type must satisfy the following additional expressions, given two values p and q that are of the type, and that np is a value of std::nullptr_t type (possibly const qualified):

pubExpressionEffects
pubType p(np);
Type p = np;
Afterwards, p is equivalent to nullptr
pubType(np)a temporary object that is equivalent to nullptr
pubp = npMust return a Type&, and afterwards, p is equivalent to nullptr
pubp != qMust return a value that is contextually convertible to bool (until C++23)
decltype(p != q) models boolean-testable (since C++23)
The effect is !(p == q)
pubp == np
np == p
Must return a value that is contextually convertible to bool (until C++23)
decltype(p == np) and decltype(np == n) each model boolean-testable (since C++23)
The effect is (p == Type())
pubp != np
np != p
Must return a value that is contextually convertible to bool (until C++23)
decltype(p != np) and decltype(np != n) each model boolean-testable (since C++23)
The effect is !(p == np)

Notes

Note that dereferencing (operator* or operator->) is not required of a NullablePointer type. A minimalistic type that satisfies these requirements is

class handle
{
int id = 0;
public:
handle() = default;
handle(std::nullptr_t) { }
explicit operator bool() const { return id != 0; }
friend bool operator==(handle l, handle r) { return l.id == r.id; }
friend bool operator!=(handle l, handle r) { return !(l == r); }
// or only a defaulted operator== (since C++20)
};

Standard library

The following types must satisfy NullablePointer:

C++ named requirements: NullablePointer (since C++11)

Specifies that the type is a pointer-like object which can be compared to std::nullptr_t objects.

Requirements

The type must meet all of the following requirements:

In addition, a value-initialized object of the type must produce a null value of that type. This null value shall only be equivalent to itself. Default initialization of the type may have an indeterminate value.

An object of the type must be contextually convertible to bool. The effect of this conversion returns false if the value is equivalent to its null value and true otherwise.

None of the operations that this type performs may throw exceptions.

The type must satisfy the following additional expressions, given two values p and q that are of the type, and that np is a value of std::nullptr_t type (possibly const qualified):

pubExpressionEffects
pubType p(np);
Type p = np;
Afterwards, p is equivalent to nullptr
pubType(np)a temporary object that is equivalent to nullptr
pubp = npMust return a Type&, and afterwards, p is equivalent to nullptr
pubp != qMust return a value that is contextually convertible to bool (until C++23)
decltype(p != q) models boolean-testable (since C++23)
The effect is !(p == q)
pubp == np
np == p
Must return a value that is contextually convertible to bool (until C++23)
decltype(p == np) and decltype(np == n) each model boolean-testable (since C++23)
The effect is (p == Type())
pubp != np
np != p
Must return a value that is contextually convertible to bool (until C++23)
decltype(p != np) and decltype(np != n) each model boolean-testable (since C++23)
The effect is !(p == np)

Notes

Note that dereferencing (operator* or operator->) is not required of a NullablePointer type. A minimalistic type that satisfies these requirements is

class handle
{
int id = 0;
public:
handle() = default;
handle(std::nullptr_t) { }
explicit operator bool() const { return id != 0; }
friend bool operator==(handle l, handle r) { return l.id == r.id; }
friend bool operator!=(handle l, handle r) { return !(l == r); }
// or only a defaulted operator== (since C++20)
};

Standard library

The following types must satisfy NullablePointer: