std::expected<T,E>::expected
来自cppreference.com
<tbody>
</tbody>
| 主模板 |
||
constexpr expected(); |
(1) | (C++23 起) |
constexpr expected( const expected& other ); |
(2) | (C++23 起) |
constexpr expected( expected&& other ) noexcept(/* 见下文 */); |
(3) | (C++23 起) |
template< class U, class G > constexpr expected( const expected<U, G>& other ); |
(4) | (C++23 起) (条件性 explicit) |
template< class U, class G > constexpr expected( expected<U, G>&& other ); |
(5) | (C++23 起) (条件性 explicit) |
template< class U = std::remove_cv_t<T> > constexpr explicit(!std::is_convertible_v<U, T>) expected( U&& v ); |
(6) | (C++23 起) |
template< class G > constexpr explicit(!std::is_convertible_v<const G&, E>) expected( const std::unexpected<G>& e ); |
(7) | (C++23 起) |
template< class G > constexpr explicit(!std::is_convertible_v<G, E>) expected( std::unexpected<G>&& e ); |
(8) | (C++23 起) |
template< class... Args > constexpr explicit expected( std::in_place_t, Args&&... args ); |
(9) | (C++23 起) |
template< class U, class... Args > constexpr explicit expected( std::in_place_t, std::initializer_list<U> il, Args&&... args ); |
(10) | (C++23 起) |
template< class... Args > constexpr explicit expected( std::unexpect_t, Args&&... args ); |
(11) | (C++23 起) |
template< class U, class... Args > constexpr explicit expected( std::unexpect_t, std::initializer_list<U> il, Args&&... args ); |
(12) | (C++23 起) |
void 部分特化 |
||
constexpr expected(); |
(13) | (C++23 起) |
constexpr expected( const expected& other ); |
(14) | (C++23 起) |
constexpr expected( expected&& other ) noexcept(std::is_nothrow_move_constructible_v<E>); |
(15) | (C++23 起) |
template< class U, class G > constexpr explicit(!std::is_convertible_v<const G&, E>) expected( const expected<U, G>& other ); |
(16) | (C++23 起) |
template< class U, class G > constexpr explicit(!std::is_convertible_v<G, E>) expected( expected<U, G>&& other ); |
(17) | (C++23 起) |
template< class G > constexpr explicit(!std::is_convertible_v<const G&, E>) expected( const std::unexpected<G>& e ); |
(18) | (C++23 起) |
template< class G > constexpr explicit(!std::is_convertible_v<G, E>) expected( std::unexpected<G>&& e ); |
(19) | (C++23 起) |
template< class... Args > constexpr explicit expected( std::in_place_t ); |
(20) | (C++23 起) |
template< class... Args > constexpr explicit expected( std::unexpect_t, Args&&... args ); |
(21) | (C++23 起) |
template< class U, class... Args > constexpr explicit expected( std::unexpect_t, std::initializer_list<U> il, Args&&... args ); |
(22) | (C++23 起) |
构造新的 expected 对象。
参数
| other | - | 另一个 expected 对象,它包含的值会被复制
|
| e | - | std::unexpected 对象,它包含的值会被复制
|
| v | - | 用来初始化包含值的值 |
| args | - | 用来初始化包含值的实参 |
| il | - | 用来初始化包含值的初始化器列表 |
效果
主模板构造函数
| 重载 | 初始化方式 | 预期值的初始化器 | 非预期值的初始化器 | 构造后的 has_value()
|
|---|---|---|---|---|
| (1) | 值初始化 | (空) | - | true
|
| (2) | (非列表) 直接初始化 |
*other
|
other.error()
|
other.has_value()
|
| (3) | std::move(*other)
|
std::move(other.error())
| ||
| (4) | std::forward<const U&>(*other)
|
std::forward<const G&>(other.error())
| ||
| (5) | std::forward<U>(*other)
|
std::forward<G>(other.error())
| ||
| (6) | std::forward<U>(v)
|
- | true
| |
| (7) | - | std::forward<const G&>(e.error())
|
false
| |
| (8) | std::forward<G>(e.error())
| |||
| (9) | std::forward<Args>(args)...
|
- | true
| |
| (10) | il,std::forward<Args>(args)...
| |||
| (11) | - | std::forward<Args>(args)...
|
false
| |
| (12) | il,std::forward<Args>(args)...
|
void 部分特化构造函数
| 重载 | 初始化方式 | 非预期值的初始化器 | 构造后的 has_value()
|
|---|---|---|---|
| (13) | 不适用 | - | true
|
| (14) | (非列表)直接初始化 | rhs.error()
|
other.has_value()
|
| (15) | std::move(rhs.error)
| ||
| (16) | std::forward<const G&>(rhs.error())
| ||
| (17) | std::forward<G>(rhs.error())
| ||
| (18) | std::forward<const G&>(e.error())
|
false
| |
| (19) | std::forward<G>(e.error())
| ||
| (20) | 不适用 | - | true
|
| (21) | (非列表)直接初始化 | std::forward<Args>(args)...
|
false
|
| (22) | il, std::forward<Args>(args)...
|
约束和补充信息
主模板构造函数
1) 此重载只有在
std::is_default_constructible_v<T> 是 true 时才会参与重载决议。2) 此构造函数被定义为弃置,除非
std::is_copy_constructible_v<T> 和 std::is_copy_constructible_v<E> 都是 true。 此构造函数是平凡的,如果
std::is_trivially_copy_constructible_v<T> 和 std::is_trivially_copy_constructible_v<E> 都是 true。3) 此重载只有在
std::is_move_constructible_v<T> 和 std::is_move_constructible_v<E> 都是 true 时才会参与重载决议。 此构造函数是平凡的,如果
std::is_trivially_move_constructible_v<T> 和 std::is_trivially_move_constructible_v<E> 都是 true。4,5) 这些重载只有在满足以下所有条件时才会参与重载决议:
- 对于重载 (4),
std::is_constructible_v<T, const U&>和std::is_constructible_v<E, const G&>都是true。 - 对于重载 (5),
std::is_constructible_v<T, U>和std::is_constructible_v<E, G>都是true。 - 如果
T不是(可有 cv 限定的)bool,那么以下 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>
- 以下 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>>
4) 此构造函数是
explicit 的,如果 std::is_convertible_v<const U&, T> 或 std::is_convertible_v<const G&, E> 是 false。5) 此构造函数是
explicit 的,如果 std::is_convertible_v<U, T> 或 std::is_convertible_v<G, E> 是 false。6) 此重载只有在满足以下所有条件时才会参与重载决议:
std::is_same_v<std::remove_cvref_t<U>, std::in_place_t>是false。std::is_same_v<std::expected<T, E>, 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的特化。
7) 此重载只有在
std::is_constructible_v<E, const G&> 是 true 时才会参与重载决议。8) 此重载只有在
std::is_constructible_v<E, G> 是 true 时才会参与重载决议。9) 此重载只有在
std::is_constructible_v<T, Args...> 是 true 时才会参与重载决议。10) 此重载只有在
std::is_constructible_v<T, std::initializer_list<U>&, Args...> 是 true 时才会参与重载决议。11) 此重载只有在
std::is_constructible_v<E, Args...> 是 true 时才会参与重载决议。12) 此重载只有在
std::is_constructible_v<E, std::initializer_list<U>&, Args...> 是 true 时才会参与重载决议。void 部分特化构造函数
14) 此构造函数被定义为弃置,除非
std::is_copy_constructible_v<E> 是 true。 此构造函数是平凡的,如果
std::is_trivially_copy_constructible_v<E> 是 true。15) 此重载只有在
std::is_move_constructible_v<E> 是 true 时才会参与重载决议。 此构造函数是平凡的,如果
std::is_trivially_move_constructible_v<E> 是 true。16,17) 这些重载只有在满足以下所有条件时才会参与重载决议:
- 对于重载 (16),
std::is_constructible_v<E, const G&>是true。 - 对于重载 (17),
std::is_constructible_v<E, G>是true。 std::is_void_v<U>是true。- 以下 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>>
18) 此重载只有在
std::is_constructible_v<E, const G&> 是 true 时才会参与重载决议。19) 此重载只有在
std::is_constructible_v<E, G> 是 true 时才会参与重载决议。21) 此重载只有在
std::is_constructible_v<E, Args...> 是 true 时才会参与重载决议。22) 此重载只有在
std::is_constructible_v<E, std::initializer_list<U>&, Args...> 是 true 时才会参与重载决议。异常
主模板构造函数
1) 抛出在初始化预期值的过程中抛出的异常。
2) 抛出在初始化预期值或非预期值的过程中抛出的异常。
3) 抛出在初始化预期值或非预期值的过程中抛出的异常。
noexcept 说明:
noexcept(std::is_nothrow_move_constructible_v<T> && std::is_nothrow_move_constructible_v<E>)4,5) 抛出在初始化预期值或非预期值的过程中抛出的异常。
6) 抛出在初始化预期值的过程中抛出的异常。
7,8) 抛出在初始化非预期值的过程中抛出的异常。
9,10) 抛出在初始化预期值的过程中抛出的异常。
11,12) 抛出在初始化非预期值的过程中抛出的异常。
void 部分特化构造函数
14-19) 抛出在初始化非预期值的过程中抛出的异常。
21,22) 抛出在初始化非预期值的过程中抛出的异常。
示例
| 本节未完成 原因:暂无示例 |
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 3886 | C++23 | 重载 (6) 的默认模板实参是 T
|
改成 std::remove_cv_t<T>
|
参阅
(C++23) |
表示一个非预期值 (类模板) |
| 原位构造标签 (类模板) | |
(C++23) |
expected 中非预期值的原位构造标签 (类) (常量) |