58 std::unique_ptr<T> ptr;
69 template <
class... Args>
71 : ptr(new
T(std::forward<Args>(args)...)) {}
79 : ptr(other.ptr ? new
T(other.
value()) : nullptr) {}
86 : ptr(std::move(other.ptr)) {}
148 ptr.
reset(other.ptr ?
new T(*other.ptr) :
nullptr);
160 ptr = std::move(other.ptr);
188 template <
class... Args>
191 ptr.reset(
new T(std::forward<Args>(args)...));
203 return os << maybe.
value();
204 return os <<
"(nothing)";
218 maybe.
reset(std::move(tmp));
231 explicit operator bool() const noexcept {
return ptr; }
260 return ptr ? *ptr :
T(std::forward<U>(default_value));
const Maybe & operator=(T &&value)
从 T 值赋值(移动)。
Definition maybe.h:134
friend std::ostream & operator<<(std::ostream &os, const Maybe &maybe)
输出 Maybe 到流。若包含值则输出值,否则输出 "(nothing)"。
Definition maybe.h:200
Maybe(Args &&...args)
从参数包直接构造一个包含值的 Maybe。
Definition maybe.h:70
Maybe & operator=(Maybe &&other) noexcept
移动赋值。
Definition maybe.h:157
Maybe & operator=(const Maybe &other)
拷贝赋值。
Definition maybe.h:145
const T & operator*() const
解引用获取值(常量),前置条件:has_value() 为 true。
Definition maybe.h:243
bool has_value() const noexcept
检查 Maybe 是否包含值。
Definition maybe.h:237
T * operator->()
成员访问运算符(非常量),前置条件:has_value() 为 true。
Definition maybe.h:246
const T * operator->() const
成员访问运算符(常量),前置条件:has_value() 为 true。
Definition maybe.h:249
T & operator*()
解引用获取值(非常量),前置条件:has_value() 为 true。
Definition maybe.h:240
friend std::istream & operator>>(std::istream &is, Maybe &maybe)
从流读取一个值到 Maybe。
Definition maybe.h:214
T value_or(U &&default_value) const
返回当前值,若为空则返回提供的默认值。
Definition maybe.h:258
void swap(Maybe &other) noexcept
交换两个 Maybe 的内容。
Definition maybe.h:267
const T & value() const
获取内部值的引用(常量)。
Definition maybe.h:111
T & value()
获取内部值的引用(非常量)。
Definition maybe.h:99
const Maybe & operator=(const T &value)
从 T 值赋值(拷贝)。
Definition maybe.h:123
Maybe(Maybe &&other)
移动构造,转移所有权。
Definition maybe.h:85
void reset(Args &&...args)
重置为新的值(从参数包构造)。
Definition maybe.h:189
Maybe(decltype(nothing))
从 nothing 标记构造空 Maybe。
Definition maybe.h:92
Maybe()
默认构造一个空 Maybe。
Definition maybe.h:62
const Maybe & operator=(decltype(nothing))
赋值为空(nothing)。
Definition maybe.h:169
Maybe(const Maybe &other)
拷贝构造,深拷贝内部值。
Definition maybe.h:78
void reset()
清空 Maybe,使其为空。
Definition maybe.h:178
表示对空的 Maybe 对象进行取值操作时抛出的异常。
Definition csexc.h:166
定义 console 库使用的自定义异常类层次结构。
struct console::@002045325103255016017112205301031250312272036216 nothing
空状态标记对象,用于显式构造空的 Maybe。