|
Console Library 4.7.0
A header-only library that makes C++ simple
|
可选值容器,可包含一个值或为空。 More...
#include <maybe.h>
Public Member Functions | |
| Maybe () | |
| 默认构造一个空 Maybe。 | |
| template<class... Args> | |
| Maybe (Args &&...args) | |
| 从参数包直接构造一个包含值的 Maybe。 | |
| Maybe (const Maybe &other) | |
| 拷贝构造,深拷贝内部值。 | |
| Maybe (Maybe &&other) | |
| 移动构造,转移所有权。 | |
| Maybe (decltype(nothing)) | |
| 从 nothing 标记构造空 Maybe。 | |
| T & | value () |
| 获取内部值的引用(非常量)。 | |
| const T & | value () const |
| 获取内部值的引用(常量)。 | |
| const Maybe & | operator= (const T &value) |
| 从 T 值赋值(拷贝)。 | |
| const Maybe & | operator= (T &&value) |
| 从 T 值赋值(移动)。 | |
| Maybe & | operator= (const Maybe &other) |
| 拷贝赋值。 | |
| Maybe & | operator= (Maybe &&other) noexcept |
| 移动赋值。 | |
| const Maybe & | operator= (decltype(nothing)) |
| 赋值为空(nothing)。 | |
| void | reset () |
| 清空 Maybe,使其为空。 | |
| template<class... Args> | |
| void | reset (Args &&...args) |
| 重置为新的值(从参数包构造)。 | |
| operator bool () const noexcept | |
| 检查 Maybe 是否包含值。 | |
| bool | has_value () const noexcept |
| 检查 Maybe 是否包含值。 | |
| T & | operator* () |
| 解引用获取值(非常量),前置条件:has_value() 为 true。 | |
| const T & | operator* () const |
| 解引用获取值(常量),前置条件:has_value() 为 true。 | |
| T * | operator-> () |
| 成员访问运算符(非常量),前置条件:has_value() 为 true。 | |
| const T * | operator-> () const |
| 成员访问运算符(常量),前置条件:has_value() 为 true。 | |
| template<class U> | |
| T | value_or (U &&default_value) const |
| 返回当前值,若为空则返回提供的默认值。 | |
| void | swap (Maybe &other) noexcept |
| 交换两个 Maybe 的内容。 | |
Friends | |
| std::ostream & | operator<< (std::ostream &os, const Maybe &maybe) |
| 输出 Maybe 到流。若包含值则输出值,否则输出 "(nothing)"。 | |
| std::istream & | operator>> (std::istream &is, Maybe &maybe) |
| 从流读取一个值到 Maybe。 | |
可选值容器,可包含一个值或为空。
| T | 存储的值类型。 |
基于 std::unique_ptr 实现,支持拷贝、移动、流输入输出。 访问空 Maybe 会抛出 bad_maybe_access 异常。
|
inline |
默认构造一个空 Maybe。
从参数包直接构造一个包含值的 Maybe。
| Args | 构造 T 所需的参数类型。 |
| args | 转发给 T 的构造函数。 |
|
inline |
拷贝构造,深拷贝内部值。
| other | 源 Maybe。 |
|
inline |
移动构造,转移所有权。
| other | 源 Maybe,移动后为空。 |
|
inline |
从 nothing 标记构造空 Maybe。
| nothing | 标记。 |
|
inlinenoexcept |
检查 Maybe 是否包含值。
|
inlineexplicitnoexcept |
检查 Maybe 是否包含值。
|
inline |
解引用获取值(非常量),前置条件:has_value() 为 true。
|
inline |
解引用获取值(常量),前置条件:has_value() 为 true。
|
inline |
成员访问运算符(非常量),前置条件:has_value() 为 true。
|
inline |
成员访问运算符(常量),前置条件:has_value() 为 true。
|
inline |
|
inline |
|
inline |
|
inlinenoexcept |
|
inline |
|
inline |
清空 Maybe,使其为空。
重置为新的值(从参数包构造)。
| Args | 参数类型。 |
| args | 转发给 T 的构造函数。 |
|
inlinenoexcept |
交换两个 Maybe 的内容。
| other | 要交换的 Maybe。 |
|
inline |
|
inline |
|
inline |
返回当前值,若为空则返回提供的默认值。
| U | 默认值类型。 |
| default_value | 默认值(可转发)。 |
|
friend |
|
friend |
从流读取一个值到 Maybe。
| is | 输入流。 |
| maybe | 目标 Maybe。 |
尝试从流读取一个 T 类型的值,若成功则存入 Maybe,否则将 Maybe 置空并清除错误标志。