std::optional<T>::operator=
来自 cppreference.cn
optional& operator=( std::nullopt_t ) noexcept; |
(1) | (since C++17) (constexpr since C++20) |
constexpr optional& operator=( const optional& other ); |
(2) | (since C++17) |
constexpr optional& operator= ( optional&& other ) noexcept(/* see below */); |
(3) | (since C++17) |
template< class U > optional& operator=( const optional<U>& other ); |
(4) | (since C++17) (constexpr since C++20) |
template< class U > optional& operator=( optional<U>&& other ); |
(5) | (since C++17) (constexpr since C++20) |
template< class U = std::remove_cv_t<T> > optional& operator=( U&& value ); |
(6) | (since C++17) (constexpr since C++20) |
用 other 的内容替换 *this 的内容。
1) 如果 *this 包含值,则调用 val()->T::~T() 来销毁所含值;否则无效果。在此调用后 *this 不包含值。
2-5) 赋值 other 的状态。在此调用后,has_value() 返回 other.has_value()。
效果 | *this 包含值 | *this 不包含值 |
---|---|---|
other 包含值 |
|
|
other 不包含值 | 通过调用 val ->T::~T() 销毁所含值 |
无效果 |
如果 std::is_trivially_copy_constructible_v<T>、std::is_trivially_copy_assignable_v<T> 和 std::is_trivially_destructible_v<T> 均为 true,则赋值运算符是平凡的。
如果 std::is_trivially_move_constructible_v<T>、std::is_trivially_move_assignable_v<T> 和 std::is_trivially_destructible_v<T> 均为 true,则赋值运算符是平凡的。
4,5) 仅当满足以下所有条件时,这些重载才参与重载决议
- 以下 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。
6) 如果 *this 包含值,则将 std::forward<U>(value) 赋值给所含值;否则使用 std::forward<U>(value) 直接非列表初始化所含值。在此调用后 *this 包含值。
仅当满足以下所有条件时,此重载才参与重载决议
- std::decay_t<U>(until C++20)std::remove_cvref_t<U>(since 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
[编辑] 异常
2-6) 抛出 T 的构造函数或赋值运算符抛出的任何异常。如果抛出异常,则 *this(以及 (2-5) 情况下的 other)的初始化状态不变,即如果对象包含值,它仍然包含值,反之亦然。value 的内容以及 *this 和 other 的所含值取决于异常来源操作(复制构造函数、移动赋值等)的异常安全性保证。
3) 具有以下
noexcept 规范:
noexcept(std::is_nothrow_move_assignable_v<T> &&
std::is_nothrow_move_constructible_v<T>)
std::is_nothrow_move_constructible_v<T>)
[编辑] 注释
可以使用 op = {}; 和 op = nullopt; 将 optional 对象 op 变成空 optional。第一个表达式使用 {} 构造一个空 optional 对象,并将其赋值给 op。
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__cpp_lib_optional |
202106L |
(C++20) (DR20) |
完全 constexpr (1), (4-6) |
[编辑] 示例
Run this code
#include <iostream> #include <optional> int main() { std::optional<const char*> s1 = "abc", s2; // constructor s2 = s1; // assignment s1 = "def"; // decaying assignment (U = char[4], T = const char*) std::cout << *s2 << ' ' << *s1 << '\n'; }
Output
abc def
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 3886 | C++17 | 重载 (6) 的默认模板实参为 T |
更改为 std::remove_cv_t<T> |
P0602R4 | C++17 | 复制/移动赋值运算符可能不是平凡的 即使底层操作是平凡的 |
需要传播平凡性 |
P2231R1 | C++20 | 重载 (1,4-6) 不是 constexpr | 改为 constexpr |
[编辑] 参见
就地构造所含值 (公共成员函数) |