std::optional<T>:: operator=
|
optional
&
operator
=
(
std::
nullopt_t
)
noexcept
;
|
(1) |
(自 C++17 起)
(自 C++20 起为 constexpr) |
|
constexpr
optional
&
operator
=
(
const
optional
&
other
)
;
|
(2) | (自 C++17 起) |
|
constexpr
optional
&
operator
=
( optional && other ) noexcept ( /* 见下文 */ ) ; |
(3) | (自 C++17 起) |
|
template
<
class
U
>
optional & operator = ( const optional < U > & other ) ; |
(4) |
(自 C++17 起)
(自 C++20 起为 constexpr) |
|
template
<
class
U
>
optional & operator = ( optional < U > && other ) ; |
(5) |
(自 C++17 起)
(自 C++20 起为 constexpr) |
|
template
<
class
U
=
std::
remove_cv_t
<
T
>
>
optional & operator = ( U && value ) ; |
(6) |
(自 C++17 起)
(自 C++20 起为 constexpr) |
将 * this 的内容替换为 other 的内容。
| 效果 | * this 包含值 | * this 不包含值 |
|---|---|---|
| other 包含值 |
|
|
| other 不包含值 |
通过调用
val
-
>
T
::
~T
(
)
销毁所含值
|
无效果 |
-
以下12个值均为
false
[1]
:
- std:: is_constructible_v < T, std:: optional < U > & >
- std:: is_constructible_v < T, const std:: optional < U > & >
- std:: is_constructible_v < T, std:: optional < U > && >
- std:: is_constructible_v < T, const std:: optional < U > && >
- std:: is_convertible_v < std:: optional < U > & , T >
- std:: is_convertible_v < const std:: optional < U > & , T >
- std:: is_convertible_v < std:: optional < U > && , T >
- std:: is_convertible_v < const std:: optional < U > && , T >
- std:: is_assignable_v < T & , std:: optional < U > & >
- std:: is_assignable_v < T & , const std:: optional < U > & >
- std:: is_assignable_v < T & , std:: optional < U > && >
- std:: is_assignable_v < T & , const std:: optional < U > && >
- 对于重载 (4) , std:: is_constructible_v < T, const U & > 和 std:: is_assignable_v < T & , const U & > 均为 true 。
- 对于重载 (5) , std:: is_constructible_v < T, U > 和 std:: is_assignable_v < T & , U > 均为 true 。
- std:: decay_t < U > (C++20 前) std:: remove_cvref_t < U > (C++20 起) 不是 std:: optional < T > 。
- std:: is_constructible_v < T, U > 为 true 。
- std:: is_assignable_v < T & , U > 为 true 。
-
满足以下任意条件:
-
T不是 标量类型 。 -
std::
decay_t
<
U
>
不是
T。
-
-
↑
换言之,
T不能从任何类型为(可能带有 const 限定符) std:: optional < U > 的表达式构造、转换或赋值
目录 |
参数
| other | - |
另一个
optional
对象,其包含的值将被赋值
|
| value | - | 将要赋值给被包含值的具体数值 |
返回值
* this
异常
T
的构造函数或赋值运算符抛出的任何异常。若抛出异常,则
*
this
(以及对于
(
2-5
)
情况下的
other
)的初始化状态保持不变,即若对象已包含值,则仍包含值,反之亦然。
value
的内容以及
*
this
和
other
所包含的值取决于引发异常的操作(拷贝构造、移动赋值等)的异常安全保证。
std:: is_nothrow_move_constructible_v < T > )
注释
一个可选对象
op
可以通过
op
=
{
}
;
和
op
=
nullopt
;
两种方式转换为空可选对象。第一个表达式使用
{
}
构造一个空的
optional
对象并将其赋值给
op
。
| 特性测试宏 | 值 | 标准 | 特性 |
|---|---|---|---|
__cpp_lib_optional
|
202106L
|
(C++20)
(DR20) |
完全 constexpr ( 1 ) , ( 4-6 ) |
示例
#include <iostream> #include <optional> int main() { std::optional<const char*> s1 = "abc", s2; // 构造函数 s2 = s1; // 赋值操作 s1 = "def"; // 退化赋值 (U = char[4], T = const char*) std::cout << *s2 << ' ' << *s1 << '\n'; }
输出:
abc def
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
| 缺陷报告 | 适用范围 | 发布时行为 | 修正后行为 |
|---|---|---|---|
| LWG 3886 | C++17 |
重载版本
(
6
)
的默认模板参数为
T
|
改为 std:: remove_cv_t < T > |
| P0602R4 | C++17 |
拷贝/移动赋值运算符可能非平凡
即使底层操作是平凡的 |
要求传播平凡性 |
| P2231R1 | C++20 | 重载版本 ( 1,4-6 ) 不是 constexpr | 改为 constexpr |
参见
|
就地构造所含值
(公开成员函数) |