std::expected<T,E>::swap
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    | constexpr void swap( expected& other ) noexcept(/*见下文*/); | (C++23 起) | |
与 other 的内容交换。
-  如果 this->has_value()和other.has_value()都为真:
-  如果 T是(可能带有 cv 限定的)void,则无效果。
- 否则,等价于 using std::swap; swap(*this, other);。
 
-  如果 
- 如果 this->has_value()和other.has_value()都为假,等价于
 using std::swap; swap(this->error(), other.error());。
-  如果 this->has_value()为假而other.has_value()为真,调用 other.swap(*this)。
-  如果 this->has_value()为真而other.has_value()为假,
-  如果 T是(可能带有 cv 限定的)void,令 unex 为表示意外值的成员,等价于:
 
-  如果 
std::construct_at(std::addressof(unex), std::move(other.unex)); std::destroy_at(std::addressof(other.unex));
- 否则,令 val为表示期望值的成员,而 unex 为表示意外值的成员,等价于:
 
if constexpr (std::is_nothrow_move_constructible_v<E>) { E temp(std::move(other.unex)); std::destroy_at(std::addressof(other.unex)); try { std::construct_at(std::addressof(other.val), std::move(val)); std::destroy_at(std::addressof(val)); std::construct_at(std::addressof(unex), std::move(temp)); } catch(...) { std::construct_at(std::addressof(other.unex), std::move(temp)); throw; } } else { T temp(std::move(val)); std::destroy_at(std::addressof(val)); try { std::construct_at(std::addressof(unex), std::move(other.unex)); std::destroy_at(std::addressof(other.unex)); std::construct_at(std::addressof(other.val), std::move(temp)); } catch(...) { std::construct_at(std::addressof(val), std::move(temp)); throw; } }
-  在任何情况下,如果没有抛出异常,在交换后 this->has_value()为假,而other.has_value()为真。
 
-  在任何情况下,如果没有抛出异常,在交换后 
此函数仅在以下条件满足时参与重载解析:
-  T是(可能带有 cv 限定的)void,或者 std::is_swappable_v<T> 为真,和
- std::is_swappable_v<E> 为真,和
-  T是(可能带有cv限定符的)void,或者std::is_move_constructible_v<T>为真,和
- std::is_move_constructible_v<E> 为真,和
-  以下至少有一个为真:
-  T是(可能带有 cv 限定的)void
- std::is_nothrow_move_constructible_v<T>
- std::is_nothrow_move_constructible_v<E>
 
-  
参数
| other | - | 要与之交换内容的 optional对象 | 
返回值
(无)
异常
如果T 是(可能带有 cv 限定的)void,则 noexcept 说明:  
否则,noexcept(
std::is_nothrow_move_constructible_v<E> && std::is_nothrow_swappable_v<E>
noexcept 说明:  
noexcept(
std::is_nothrow_move_constructible_v<T> && std::is_nothrow_swappable_v<T> &&
std::is_nothrow_move_constructible_v<E> && std::is_nothrow_swappable_v<E>
在抛出异常的情况下,*this 和 other 的包含值的状态由 swap 或 T 和 E 的移动构造函数的异常安全保证决定,取决于哪个被调用。对于 *this 和 other,如果对象包含了期望值,它就保留期望值,反之亦然。
示例
| 本节未完成 原因:暂无示例 | 
参阅
| (C++23) | 特化 std::swap 算法 (函数) |