Namespaces
Variants

std::expected<T,E>:: expected

From cppreference.net
Utilities library
主模板
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 起)
(条件性显式)
template < class U, class G >
constexpr expected ( expected < U, G > && other ) ;
(5) (C++23 起)
(条件性显式)
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 起)
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 对象。

目录

参数

其他 - 另一个 expected 对象,其包含的值将被复制
e - std::unexpected 对象,其包含的值将被复制
v - 用于初始化所含值的值
args - 用于初始化所含值的参数
il - 用于初始化所含值的初始化列表

效果

主模板构造函数

重载版本 初始化
方式
初始化器用于... has_value()
构造后的状态
期望值 非期望值
( 1 ) 值初始化 (空) - true
( 2 ) 直接初始化
(非列表形式)
* other other. error ( ) other. has_value ( )
  • true ,仅初始化期望值。
  • false ,仅初始化非期望值。
( 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 ) N/A - true
( 14 ) 直接初始化(非列表) rhs. error ( ) other. has_value ( )
  • 若为 false ,则初始化异常值
( 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 ) N/A - 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 时,该构造函数是平凡(trivial)的。
4,5) These overloads participate in overload resolution only if all following conditions are satisfied:
4) std:: is_convertible_v < const U & , T > std:: is_convertible_v < const G & , E > false 时,该构造函数为 explicit
5) std:: is_convertible_v < U, T > std:: is_convertible_v < G, E > false 时,该构造函数为 explicit
6) 此重载仅在满足以下所有条件时参与重载决议:
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) 这些重载仅在满足以下所有条件时才参与重载决议:
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) 抛出由期望值或非期望值初始化过程中引发的任何异常。
4,5) 抛出由期望值或非期望值初始化所抛出的任何异常。
6) 抛出期望值初始化过程中抛出的任何异常。
7,8) 抛出由意外值初始化所抛出的任何异常。
9,10) 抛出由期望值初始化抛出的任何异常。
11,12) 抛出由意外值初始化所抛出的任何异常。

void 部分特化构造函数

14-19) 抛出由异常值初始化所抛出的任何异常。
21,22) 抛出由意外值初始化所抛出的任何异常。

示例

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

DR 适用版本 发布行为 正确行为
LWG 3886 C++23 重载函数 ( 6 ) 的默认模板参数为 T 修改为 std:: remove_cv_t < T >

另请参阅

(C++23)
表示意外值
(类模板)
原位构造标签
(标签)
用于 expected 中意外值的原位构造标签
(标签)