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 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
>
)
|
(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
|
(10) | (自 C++23 起) |
|
template
<
class
...
Args
>
constexpr explicit expected ( std:: unexpect_t , Args && ... args ) ; |
(11) | (自 C++23 起) |
|
template
<
class
U,
class
...
Args
>
constexpr
explicit
|
(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
对象。
目录 |
参数
| 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 ) ... |
约束条件与补充信息
主模板构造函数
- 对于重载 (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 >>
- 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的特化。
void 部分特化构造函数
- 对于重载 (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 >>
异常
主模板构造函数
&& std:: is_nothrow_move_constructible_v < E > )
void 部分特化构造函数
示例
|
本节内容不完整
原因:缺少示例 |
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 3886 | C++23 |
重载版本
(
6
)
的默认模板参数为
T
|
修改为 std:: remove_cv_t < T > |
参见
|
(C++23)
|
表示意外值的包装器
(类模板) |
|
原位构造标签
(标签) |
|
|
(C++23)
|
用于在
expected
中原位构造意外值的标签
(标签) |