|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Specifies that the type is a pointer-like object which can be compared tostd::nullptr_t objects.
Contents |
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 anindeterminate or erroneous(since C++26) value.
A value of the type must becontextually convertible tobool. The effect of this conversion returnsfalse if the value is equivalent to its null value andtrue otherwise.
None of the operations that this type performs may throw exceptions.
The type must satisfy the following additional expressions, given two valuesp andq that are of the type, and thatnp is a value ofstd::nullptr_t type (possibly const-qualified):
| Declaration | Effects | ||||
| Type p(np); Type p= np; | Afterwards,p is equivalent tonullptr | ||||
| Expression | Effects | ||||
| Type(np) | A temporary object that is equivalent tonullptr | ||||
| p= np | Must return aType&, and afterwards,p is equivalent tonullptr | ||||
| p!= q |
The effect is!(p== q) | ||||
| p== np np== p |
The effect is(p== Type()) | ||||
| p!= np np!= p |
The effect is!(p== np) |
Note that dereferencing (operator* oroperator->) is not required for aNullablePointer type. A minimalistic type that satisfies these requirements is
class handle{int id=0;public: handle()=default; handle(std::nullptr_t){}explicit operatorbool()const{return id!=0;}friendbool operator==(handle l, handle r){return l.id== r.id;}friendbool operator!=(handle l, handle r){return!(l== r);}// or only a defaulted operator== (since C++20)};
The following types satisfyNullablePointer:
The following types must satisfyNullablePointer in order to communicate with standard library components:
X::pointer,X::const_pointer,X::void_pointer andX::const_void_pointer of everyAllocator typeX.pointer ofstd::unique_ptr.
| (since C++23) |
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2114 (P2167R3) | C++11 | contextual convertibility tobool was too weak to reflect the expectation of implementations | requirements strengthened |