NullablePointer
C++概念:自C++11%29以来NullablePoter%28
指定该类型是一个类似指针的对象,可与std::nullptr_t
物品。
所需
该类型必须满足以下所有概念要求:
EqualityComparable
DefaultConstructible
CopyConstructible
CopyAssignable
Destructible
此外,类型的值初始化对象必须生成该类型的空值。此空值只能与其本身等效。类型的默认初始化可能具有不确定值。
类型的对象必须在上下文上可转换为bool。如果该值与其空值等效,则此转换的效果返回false,否则返回true。
此类型执行的任何操作都不会引发异常。
类型必须满足以下附加表达式,给定两个值p
和q
那种类型的np
是std::nullptr_t
类型%28可能const
合格%29:
Expression | Effects |
---|---|
Type p(np Type p = np; | Afterwards, p is equivalent to nullptr |
Type(np) | a temporary object that is equivalent to nullptr |
p = np | Must return a Type&, and afterwards, p is equivalent to nullptr |
p != q | Must return a value that is contextually convertible to bool. The effect is !(p == q). |
p == np np == p. | Must return a value that is contextually convertible to bool. The effect is (p == Type()). |
p != np np != p. | Must return a value that is contextually convertible to bool. The effect is !(p == np). |
注记
注意取消引用%28operator*或operator->%29不需要NullablePointertype。满足这些要求的极简型是。
二次
class handle {
int id;
public:
handle(std::nullptr_t = nullptr) : id(0) { }
explicit operator bool() { 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 }
};
二次
标准库
以下类型必须满足NullablePointer
*
- 成员类型
X::pointer
,,,X::const_pointer
,,,X::void_pointer
和X::const_void_pointer
每一个Allocator
类型X
- 成员类型
X::pointer
成std::unique_ptr
- 类型
std::exception_ptr
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。