std::expected<T,E>::expected
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 explicit(/* 见下文 */) expected( const expected<U, G>& other ); |
(4) | (C++23 起) |
template<
class
U, class
G >
constexpr explicit(/* 见下文 */) 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 起) |
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 起) |
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, |
(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, |
(12) | (C++23 起) |
构造新的 expected
对象。
T
不是(可有
cv 限定的)void,那么构造一个对象,它包含值初始化的期待的值。
构造完成后,has_value()
返回 true。
- 此重载只有在
T
是(可有 cv 限定的)void 或 std::is_default_constructible_v<T> 是 true时才会参与重载决议。
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。
- 要么
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。
-
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
不是(可有 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>
- 要么
- 这些构造函数是 explicit 的,如果 std::is_convertible_v<UF, T> 或 std::is_convertible_v<GF, E> 是 false。
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::is_constructible_v<T, U> 是 true。
- std::remove_cvref_t<U>
不是
std::unexpected
的特化。 - 如果
T
是(可有 cv 限定的)bool,那么 std::remove_cvref_t<U> 不是std::expected
的特化。
-
GF
为 const
G&(对于重载(7))和 G
(对于重载(8))。
构造包含不期待的值的对象,包含的值从 std::forward<GF>(e.error()) 直接初始化。
构造完成后,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 | - | 初始化所含值所用的初始化器列表 |
异常
T
的构造函数所抛的异常。
T
或
E
的构造函数所抛的异常。T
是(可有 cv
限定的)void, && std::is_nothrow_move_constructible_v<E>)
T
或
E
的构造函数所抛的异常。T
的构造函数所抛的异常。
E
的构造函数所抛的异常。T
的构造函数所抛的异常。E
的构造函数所抛的异常。示例
本节未完成 原因:暂无示例 |
参阅
(C++23)
|
表示为不期待的值 (类模板) |
原位构造标签 (类模板) |
|
(C++23)
|
expected 中不期待的值的原位构造标签 (类) (常量) |