[optional.swap] # 22 General utilities library [[utilities]](./#utilities) ## 22.5 Optional objects [[optional]](optional#swap) ### 22.5.3 Class template optional [[optional.optional]](optional.optional#optional.swap) #### 22.5.3.5 Swap [optional.swap] [🔗](#lib:swap,optional) `constexpr void swap(optional& rhs) noexcept(see below); ` [1](#1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3999) *Mandates*: is_move_constructible_v is true[.](#1.sentence-1) [2](#2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4003) *Preconditions*: T meets the [*Cpp17Swappable*](swappable.requirements#:Cpp17Swappable "16.4.4.3 Swappable requirements [swappable.requirements]") requirements ([[swappable.requirements]](swappable.requirements "16.4.4.3 Swappable requirements"))[.](#2.sentence-1) [3](#3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4007) *Effects*: See Table [71](#tab:optional.swap "Table 71: optional​::​swap(optional&) effects")[.](#3.sentence-1) Table [71](#tab:optional.swap) — optional​::​swap(optional&) effects [[tab:optional.swap]](./tab:optional.swap) | [🔗](#tab:optional.swap-row-1) | | ***this contains a value** | ***this does not contain a value** | | --- | --- | --- | --- | | [🔗](#tab:optional.swap-row-2)
**rhs contains a value** | | calls swap(*(*this), *rhs) | direct-non-list-initializes the contained value of *this with std​::​move(*rhs), followed by rhs.val->T​::​~T(); postcondition is that *this contains a value and rhs does not contain a value | | [🔗](#tab:optional.swap-row-3)
**rhs does not contain a value** | | direct-non-list-initializes the contained value of rhs with std​::​move(*(*this)), followed by val->T​::​~T(); postcondition is that *this does not contain a value and rhs contains a value | no effect | [4](#4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4030) *Throws*: Any exceptions thrown by the operations in the relevant part of Table [71](#tab:optional.swap "Table 71: optional​::​swap(optional&) effects")[.](#4.sentence-1) [5](#5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4034) *Remarks*: The exception specification is equivalent to:is_nothrow_move_constructible_v && is_nothrow_swappable_v [6](#6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4041) If any exception is thrown, the results of the expressions this->has_value() and rhs.has_value() remain unchanged[.](#6.sentence-1) If an exception is thrown during the call to function swap, the state of *val and *rhs.val is determined by the exception safety guarantee of swap for lvalues of T[.](#6.sentence-2) If an exception is thrown during the call to T's move constructor, the state of *val and *rhs.val is determined by the exception safety guarantee of T's move constructor[.](#6.sentence-3)