std::optional<T>::swap
来自cppreference.com
| void swap( optional& other ) noexcept(/* see below */); |
(C++17 起) | |
与 other 交换内容。
- 若 *this 和
other均不含值,则函数无效果。
- 若 *this 与
other仅有一个含值(称此对象为in,另一者为un),则从 std::move(*in) 直接初始化un所含值,随后如同通过 in->T::~T() 析构in所含值。此调用后,in不含值;un含值。
- 若 *this 与
other均含值,则通过调用 std::swap(**this, *other) 交换所含值。T左值必须满足可交换 (Swappable) 。
若 std::is_move_constructible_v<T> 为 false 则程序为谬构。
参数
| other | - | 要交换内容的 optional 对象
|
返回值
(无)
异常
noexcept 规定:
noexcept(std::is_nothrow_move_constructible_v<T> &&
std::is_nothrow_swappable_v<T>)
std::is_nothrow_swappable_v<T>)
在抛异常的情况下, *this 和 other 所含值的状态由 T 的 swap 或 T 的移动构造函数的异常安全保证确定,取决于所调用者。对于 *this 和 other ,若对象含值,则令它继续含值,反之亦然。
参阅
| (C++17) |
特化 std::swap 算法 (函数) |