std::expected<T,E>::expected
|
constexpr expected() noexcept(/*see below*/);
|
(1) | (C++23 起) |
|
constexpr expected( const expected& other );
|
(2) | (C++23 起) |
|
constexpr expected( expected&& other ) noexcept(/*see below*/);
|
(3) | (C++23 起) |
|
template< class U, class G >
constexpr explicit(/*see below*/) expected( const expected<U, G>& other ); |
(4) | (C++23 起) |
|
template< class U, class G >
constexpr explicit(/*see below*/) expected( expected<U, G>&& other ); |
(5) | (C++23 起) |
|
template< class U = T >
constexpr explicit(!std::is_convertible_v<U, T>) expected( U&& v ); |
(6) | (C++23 起) (T 不是 cv void) |
|
template< class G >
constexpr explicit(!std::is_convertible_v<const G&, E>) |
(7) | (C++23 起) |
|
template< class G >
constexpr explicit(!std::is_convertible_v<G, E>) |
(8) | (C++23 起) |
|
constexpr explicit expected( std::in_place_t ) noexcept;
|
(9) | (C++23 起) (T 是 cv void) |
|
template< class... Args >
constexpr explicit expected( std::in_place_t, Args&&... args ); |
(10) | (C++23 起) (T 不是 cv void) |
|
template< class U, class... Args >
constexpr explicit expected( std::in_place_t, |
(11) | (C++23 起) (T 不是 cv void) |
|
template< class... Args >
constexpr explicit expected( std::unexpect_t, Args&&... args ); |
(12) | (C++23 起) |
|
template< class U, class... Args >
constexpr explicit expected( std::unexpect_t, |
(13) | (C++23 起) |
构造新的 expected 对象。
1) 默认构造函数。如果
T 不是(可能有 cv 限定的) void ,构造一个对象,其含有值初始化的期待的值。
构造完成后, 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 为 std::add_lvalue_reference_t<const U> (对于重载(4))和 U (对于重载(5)),且
- GF 为 const G& (对于重载(4))和 G (对于重载(5))。
如果 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和 std::unexpected<E> 都不能从任何类型为(可能为 const 的) std::expected<U,G> 的表达式构造或转换。换句话说,以下12个类型特性都为 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::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>
- 要么
- 这些构造函数为 explicit ,如果 std::is_convertible_v<UF, T> 或 std::is_convertible_v<GF, E> 为 false 。
6) 构造含有期待的值的对象,如同以表达式 std::forward<U>(v) 直接初始化(但不是直接列表初始化)类型为
T 的对象一样初始化所含的值。
构造完成后, 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::remove_cvref_t<U> 不是 std::unexpected 的特化,且
- std::is_constructible_v<T, U> 为 true 。
7,8) 令 GF 为 const G& (对于重载(7))和 G (对于重载(8))。
构造含有不期待的值的对象,所含的值从 std::forward<GF>(e.error()) 直接初始化。
构造完成后, has_value() 返回 false 。
- 这些重载只有在std::is_constructible_v<E, GF> 为 true 时才参与重载决议。
9) 构造含有期待的值的对象。构造完成后,
has_value() 返回 true 。10) 构造含有期待的值的对象,所含的值从 std::forward<Args>(args)... 直接初始化。
构造完成后, has_value() 返回 true 。
- 此重载只有在std::is_constructible_v<T, Args...> 为 true 时才参与重载决议。
11) 构造含有期待的值的对象,所含的值从 il, std::forward<Args>(args)... 直接初始化。
构造完成后, has_value() 返回 true 。
- 此重载只有在std::is_constructible_v<T, std::initializer_list<U>&, Args...> 为 true 时才参与重载决议。
12) 构造含有不期待的值的对象,所含的值从 std::forward<Args>(args)... 直接初始化。
构造完成后, has_value() 返回 false 。
- 此重载只有在std::is_constructible_v<E, Args...> 为 true 时才参与重载决议。
13) 构造含有不期待的值的对象,所含的值从 il, std::forward<Args>(args)... 直接初始化。
构造完成后, has_value() 返回 false 。
- 此重载只有在std::is_constructible_v<E, std::initializer_list<U>&, Args...> 为 true 时才参与重载决议。
参数
| other | - | 另一个 expected 对象,其所含的值被复制 |
| e | - | std::unexpected 对象,其所含的值被复制 |
| v | - | 初始化所含值所用的值 |
| args... | - | 初始化所含值所用的参数 |
| il | - | 初始化所含值所用的初始化器列表 |
异常
2-5) 抛出任何
T 或 E 的构造函数所抛的异常。3) 如果
T 是(可能有 cv 限定的) void,
noexcept 说明:
否则,
noexcept(std::is_nothrow_move_constructible_v<E>)
noexcept 说明:
noexcept(std::is_nothrow_move_constructible_v<T> && std::is_nothrow_move_constructible_v<E>)
6,10-11) 抛出任何
T 的构造函数所抛的异常。7-8,12-13) 抛出任何
E 的构造函数所抛的异常。9)
noexcept 说明:
noexcept
Example
| 本节未完成 原因:暂无示例 |
See also
|
(C++23)
|
表示为不期待的值 (类模板) |
| 原位构造标签 (类模板) |
|
|
(C++23)
|
expected 中不期待的值的原位构造标签(类) (常量) |