42#if __cplusplus >= 201703L
56 template <
class T,
class E>
59 !std::is_same<T, E>::value,
"T and E cannot be the same type");
63 =
sizeof(
T) >
sizeof(E) ?
sizeof(
T) : sizeof(
E);
66 =
alignof(
T) >
alignof(
E) ?
alignof(
T) :
alignof(
E);
131 new (
get_ok_ptr())
T(std::move(*other.get_ok_ptr()));
142 if (
this != &other)
Result(other).
swap(*
this);
152 if (
this != &other)
Result(std::move(other)).swap(*
this);
173 assert(
is_ok_ &&
"Called get_ok on error");
184 assert(!
is_ok_ &&
"Called get_err on success");
195 assert(
is_ok_ &&
"Called get_ok on error");
206 assert(!
is_ok_ &&
"Called get_err on success");
253 template <
class F,
class G>
269 typename std::decay<
decltype(f(
270 std::declval<T>()))>::type::value_type,
284 typename std::decay<
decltype(f(
285 std::declval<T>()))>::type::value_type,
300 typename std::decay<
decltype(f(
301 std::declval<E>()))>::type::error_type> {
315 typename std::decay<
decltype(f(
316 std::declval<E>()))>::type::error_type> {
326 if (
this == &other)
return;
327 if (
is_ok_ && other.is_ok_) {
329 *
get_ok_ptr() = std::move(*other.get_ok_ptr());
330 *other.get_ok_ptr() = std::move(temp);
331 }
else if (!
is_ok_ && !other.is_ok_) {
334 *other.get_err_ptr() = std::move(temp);
339 other.get_err_ptr()->~E();
340 new (other.get_ok_ptr())
T(std::move(temp_t));
344 new (
get_ok_ptr())
T(std::move(*other.get_ok_ptr()));
345 other.get_ok_ptr()->~T();
346 new (other.get_err_ptr())
E(std::move(temp_e));
348 std::swap(
is_ok_, other.is_ok_);
362 explicit operator bool()
const {
return is_ok_; }
448#if __cplusplus >= 201703L
454 std::optional<T> as_optional() const & {
463 std::optional<T> as_optional() && {
T unwrap_or_else(F &&fn) const &
如果是Ok则返回Ok值,否则通过函数生成默认值。
Definition result.h:394
static constexpr size_t buffer_align
缓冲区对齐要求,取 T 和 E 中较大者。
Definition result.h:66
T unwrap_or_else(F &&fn) &&
如果是Ok则移动返回值,否则通过函数生成默认值。
Definition result.h:404
const E * err_ptr() const
获取Err值的指针(可能为空)。
Definition result.h:424
T unwrap() const
解包成功值,若为错误则抛出异常。
Definition result.h:216
static constexpr size_t buffer_size
缓冲区大小,取 T 和 E 中较大者。
Definition result.h:63
T expect(const std::string &msg) const
带自定义消息的解包,若为错误则抛出异常。
Definition result.h:228
E * err_ptr()
获取Err值的指针(可能为空)。
Definition result.h:430
void visit(F &&f, G &&g) const
访问 Result 的内容,根据状态调用不同的函数。
Definition result.h:254
bool operator==(const Result &other) const
相等性比较。
Definition result.h:436
T unwrap_or(T &&default_value) &&
如果是Ok则移动返回值,否则返回默认值。
Definition result.h:384
T ok() &&
移动获取成功值(右值引用版本)。
Definition result.h:194
auto or_else(F &&f) const &-> Result< T, typename std::decay< decltype(f(std::declval< E >()))>::type::error_type >
链式操作:若为 Err 则应用 f,否则传递 Ok(左值引用版本)。
Definition result.h:299
T * ok_ptr()
获取Ok值的指针(可能为空)。
Definition result.h:418
~Result()
析构函数,正确销毁当前存储的值。
Definition result.h:159
Result(const E &error)
从错误值构造 Result(拷贝)。
Definition result.h:94
Result & operator=(const Result &other)
拷贝赋值(使用 copy-and-swap 惯用法)。
Definition result.h:141
T value_type
成功值类型别名。
Definition result.h:80
const T & unwrap_or(const T &default_value) const &
如果是Ok则返回值的引用,否则返回默认值的引用。
Definition result.h:375
unsigned char buffer[buffer_size]
对齐的原始字节缓冲区,用于存储 T 或 E。
Definition result.h:69
friend void swap(Result &a, Result &b) noexcept
交换两个 Result 的内容(友元函数)。
Definition result.h:356
Result(T &&value)
从成功值构造 Result(移动)。
Definition result.h:100
const T * ok_ptr() const
获取Ok值的指针(可能为空)。
Definition result.h:412
E * get_err_ptr() const
获取指向错误值 E 的指针(常量)。
Definition result.h:76
T ok() const &
获取成功值的副本(左值引用版本)。
Definition result.h:172
auto and_then(F &&f) const &-> Result< typename std::decay< decltype(f(std::declval< T >()))>::type::value_type, E >
链式操作:若为 Ok 则应用 f,否则传递 Err(左值引用版本)。
Definition result.h:268
T * get_ok_ptr() const
获取指向成功值 T 的指针(常量)。
Definition result.h:74
void swap(Result &other) noexcept
交换两个 Result 的内容。
Definition result.h:325
Result(Result &&other) noexcept
移动构造,转移内部值。
Definition result.h:129
Result(const Result &other)
拷贝构造,深拷贝内部值。
Definition result.h:117
bool is_ok_
标记当前是否为 Ok 状态(true 表示 Ok,false 表示 Err)。
Definition result.h:71
E err() const &
获取错误值的副本(左值引用版本)。
Definition result.h:183
E err() &&
移动获取错误值(右值引用版本)。
Definition result.h:205
Result & operator=(Result &&other) noexcept
移动赋值(使用 copy-and-swap 惯用法)。
Definition result.h:151
bool is_ok() const
检查是否为 Ok(成功)状态。
Definition result.h:237
bool operator!() const
逻辑非,检查是否为 Err。
Definition result.h:368
bool operator!=(const Result &other) const
不等性比较。
Definition result.h:446
bool is_err() const
检查是否为 Err(错误)状态。
Definition result.h:243
Result(const T &value)
从成功值构造 Result(拷贝)。
Definition result.h:88
E error_type
错误值类型别名。
Definition result.h:82
auto or_else(F &&f) &&-> Result< T, typename std::decay< decltype(f(std::declval< E >()))>::type::error_type >
链式操作:若为 Err 则应用 f,否则传递 Ok(右值引用版本)。
Definition result.h:314
auto and_then(F &&f) &&-> Result< typename std::decay< decltype(f(std::declval< T >()))>::type::value_type, E >
链式操作:若为 Ok 则应用 f,否则传递 Err(右值引用版本)。
Definition result.h:283
Result(E &&error)
从错误值构造 Result(移动)。
Definition result.h:108
ValueError(const std::string &msg)
构造 ValueError。
Definition csexc.h:115
定义 console 库使用的自定义异常类层次结构。