[expected.object.swap]

# 22 General utilities library [[utilities]](./#utilities)

## 22.8 Expected objects [[expected]](expected#object.swap)

### 22.8.6 Class template expected [[expected.expected]](expected.expected#expected.object.swap)

#### 22.8.6.5 Swap [expected.object.swap]

[ð](#lib:swap,expected)

`constexpr void swap(expected& rhs) noexcept(see below);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L8800)

*Constraints*: 

- [(1.1)](#1.1)

is_swappable_v<T> is true and

- [(1.2)](#1.2)

is_swappable_v<E> is true and

- [(1.3)](#1.3)

is_move_constructible_v<T> && is_move_constructible_v<E> is true, and

- [(1.4)](#1.4)

is_nothrow_move_constructible_v<T> || is_nothrow_move_constructible_v<E> is true[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L8815)

*Effects*: See Table [72](#tab:expected.object.swap "Table 72: swap(expected&amp;) effects")[.](#2.sentence-1)

Table [72](#tab:expected.object.swap) — swap(expected&) effects [[tab:expected.object.swap]](./tab:expected.object.swap)  

| [ð](#tab:expected.object.swap-row-1) | **this->has_value()** | **!this->has_value()** |
| --- | --- | --- |
| [ð](#tab:expected.object.swap-row-2)<br>**rhs.has_value()** | equivalent to: using std​::​swap; swap(*val*, rhs.*val*); | calls rhs.swap(*this) |
| [ð](#tab:expected.object.swap-row-3)<br>**!rhs.has_value()** | *see below* | equivalent to: using std​::​swap; swap(*unex*, rhs.*unex*); |

For the case where rhs.has_value() is false andthis->has_value() is true, equivalent to:if constexpr (is_nothrow_move_constructible_v<E>) { E tmp(std::move(rhs.*unex*));
 destroy_at(addressof(rhs.*unex*)); try { construct_at(addressof(rhs.*val*), std::move(*val*));
 destroy_at(addressof(*val*));
 construct_at(addressof(*unex*), std::move(tmp)); } catch(...) { construct_at(addressof(rhs.*unex*), std::move(tmp)); throw; }} else { T tmp(std::move(*val*));
 destroy_at(addressof(*val*)); try { construct_at(addressof(*unex*), std::move(rhs.*unex*));
 destroy_at(addressof(rhs.*unex*));
 construct_at(addressof(rhs.*val*), std::move(tmp)); } catch (...) { construct_at(addressof(*val*), std::move(tmp)); throw; }}*has_val* = false;
rhs.*has_val* = true;

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L8861)

*Throws*: Any exception thrown by the expressions in the *Effects*[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L8865)

*Remarks*: The exception specification is equivalent to:is_nothrow_move_constructible_v<T> && is_nothrow_swappable_v<T> && is_nothrow_move_constructible_v<E> && is_nothrow_swappable_v<E>

[ð](#lib:swap,expected_)

`friend constexpr void swap(expected& x, expected& y) noexcept(noexcept(x.swap(y)));
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L8880)

*Effects*: Equivalent to x.swap(y)[.](#5.sentence-1)
