std::expected<T,E>::expected
来自cppreference.com
| |
(1) | (C++23 起) |
| |
(2) | (C++23 起) |
| |
(3) | (C++23 起) |
| |
(4) | (C++23 起) |
| |
(5) | (C++23 起) |
| (6) | (C++23 起) (T 不是 cv void) |
|
| |
(7) | (C++23 起) |
| |
(8) | (C++23 起) |
| |
(9) | (C++23 起) (T 不是 cv void) |
| |
(10) | (C++23 起) (T 不是 cv void) |
| |
(11) | (C++23 起) (T 是 cv void) |
| |
(12) | (C++23 起) |
| |
(13) | (C++23 起) |
构造新的 expected 对象。
构造完成后,
has_value() 返回 true。 此重载只有在
T 是(可能有 cv 限定的)void,或者 std::is_default_constructible_v<T> 为 true 时才会参与重载决议。2) 复制构造函数。如果
other.has_value() 为 false,则新对象包含一个非预期值,该值从 other.error() 直接初始化。否则,如果 T 不是(可能有 cv 限定的)void,则新对象包含一个预期值,该值从 *other 直接初始化。 构造完成后,
has_value() 等于 other.has_value()。 该构造函数被定义为弃置的,除非满足以下条件:
- 要么
T是(可能有 cv 限定的)void,要么std::is_copy_constructible_v<T>为true,且 std::is_copy_constructible_v<E>为true。
如果满足以下条件,则该构造函数是平凡的:
- 要么
T是(可能有 cv 限定的)void,要么std::is_trivially_copy_constructible_v<T>为true,且 std::is_trivially_copy_constructible_v<E>为true。
3) 移动构造函数。如果
other.has_value() 为 false,则新对象包含一个非预期值,该值从 std::move(other.error()) 直接初始化。否则,如果 T 不是(可能有 cv 限定的)void,则新对象包含一个预期值,该值从 std::move(*other) 直接初始化。 构造完成后,
has_value() 等于 other.has_value()。 仅当满足以下条件时,该构造函数才参与重载决议:
- 要么
T是(可能有 cv 限定的)void,要么std::is_move_constructible_v<T>为true,且 std::is_move_constructible_v<E>为true。
如果满足以下条件,则该构造函数是平凡的:
std::is_trivially_move_constructible_v<T>为true,且std::is_trivially_move_constructible_v<E>为true。
4,5) 令
UF对于 (4) 为std::add_lvalue_reference_t<const U>,对于 (5) 为U,且GF对于 (4) 为const G&,对于 (5) 为G。
如果
other.has_value() 为 false,则新对象包含一个非预期值,该值从 std::forward<GF>(other.error()) 直接初始化。否则,如果 T 不是(可能有 cv 限定的)void,则新对象包含一个预期值,该值从 std::forward<UF>(*other) 直接初始化。 构造完成后,
has_value() 等于 other.has_value()。 这些构造函数各自仅在分别满足以下条件时参与重载决议:
- 要么
T是(可能有 cv 限定的)void,且std::is_void_v<U>为true,要么std::is_constructible_v<T, UF>为true。
std::is_constructible_v<E, GF>为true。- 如果
T不是(可能有 cv 限定的)bool,则T不能从任何类型为(可能有const的)std::expected<U, G>的表达式构造或转换,即以下 8 个值均为false:std::is_constructible_v<T, std::expected<U, G>&std::is_constructible_v<T, std::expected<U, G>std::is_constructible_v<T, const std::expected<U, G>&std::is_constructible_v<T, const std::expected<U, G>std::is_convertible_v<std::expected<U, G>&, T>std::is_convertible_v<std::expected<U, G>, T>std::is_convertible_v<const std::expected<U, G>&, T>std::is_convertible_v<const std::expected<U, G>, T>
std::unexpected<E>不能从任何类型为(可能有const的)std::expected<U, G>的表达式构造,即以下 4 个值均为false:std::is_constructible_v<std::unexpected<E>, std::expected<U, G>&std::is_constructible_v<std::unexpected<E>, std::expected<U, G>std::is_constructible_v<std::unexpected<E>, const std::expected<U, G>&std::is_constructible_v<std::unexpected<E>, const std::expected<U, G>
如果
std::is_convertible_v<UF, T> 或 std::is_convertible_v<GF, E> 为 false,则这些构造函数为 explicit。 构造完成后,
has_value() 返回 true。 除非满足以下条件,否则该构造函数不参与重载决议:
T不是(可能有 cv 限定的)void。std::is_same_v<std::remove_cvref_t<U>, std::in_place_t>为false。std::is_same_v<expected, std::remove_cvref_t<U>>为false。std::is_constructible_v<T, U>为true。std::remove_cvref_t<U>不是std::unexpected的特化。- 如果
T是(可能有 cv 限定的)bool,则std::remove_cvref_t<U>不是std::expected的特化。
构造完成后,
has_value() 返回 false。 这些重载只有在
std::is_constructible_v<E, GF> 为 true 时才会参与重载决议。 构造完成后,
has_value() 返回 true。 此重载只有在
std::is_constructible_v<T, Args...> 为 true 时才会参与重载决议。 构造完成后,
has_value() 返回 true。 此重载只有在
std::is_constructible_v<T, std::initializer_list<U>&, Args...> 为 true 时才会参与重载决议。 构造完成后,
has_value() 返回 false。 此重载只有在
std::is_constructible_v<E, Args...> 为 true 时才会参与重载决议。 构造完成后,
has_value() 返回 false。 此重载只有在
std::is_constructible_v<E, std::initializer_list<U>&, Args...> 为 true 时才会参与重载决议。参数
| other | - | 另一个 expected 对象,它包含的值会被复制
|
| e | - | std::unexpected 对象,它包含的值会被复制
|
| v | - | 用来初始化包含值的值 |
| args... | - | 用来初始化包含值的实参 |
| il | - | 用来初始化包含值的初始化器列表 |
异常
1) 抛出由
T 的构造函数抛出的任何异常。2) 抛出由
T 或 E 的构造函数抛出的任何异常。 否则,
noexcept 说明:
noexcept(std::is_nothrow_move_constructible_v<T>
&& std::is_nothrow_move_constructible_v<E>)
4,5) 抛出由
T 或 E 的构造函数抛出的任何异常。6) 抛出由
T 的构造函数抛出的任何异常。7,8) 抛出由
E 的构造函数抛出的任何异常。9,10) 抛出由
T 的构造函数抛出的任何异常。12,13) 抛出由
E 的构造函数抛出的任何异常。示例
| 本节未完成 原因:暂无示例 |
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 3886 | C++23 | 重载 (6) 的默认模板实参是 T
|
改成 std::remove_cv_t<T>
|
参阅
(C++23) |
表示一个非预期值 (类模板) |
| 原位构造标签 (类模板) | |
(C++23) |
expected 中非预期值的原位构造标签 (类) (常量) |