Files
2025-10-25 03:02:53 +03:00

67 lines
2.5 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[variant.swap]
# 22 General utilities library [[utilities]](./#utilities)
## 22.6 Variants [[variant]](variant#swap)
### 22.6.3 Class template variant [[variant.variant]](variant.variant#variant.swap)
#### 22.6.3.7 Swap [variant.swap]
[🔗](#lib:swap,variant)
`constexpr void swap(variant& rhs) noexcept(see below);
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6407)
*Mandates*: is_move_constructible_v<Ti> is true for all i[.](#1.sentence-1)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6411)
*Preconditions*: Each Ti meets the [*Cpp17Swappable*](swappable.requirements#:Cpp17Swappable "16.4.4.3Swappable requirements[swappable.requirements]") requirements ([[swappable.requirements]](swappable.requirements "16.4.4.3Swappable requirements"))[.](#2.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6415)
*Effects*:
- [(3.1)](#3.1)
If valueless_by_exception() && rhs.valueless_by_exception() no effect[.](#3.1.sentence-1)
- [(3.2)](#3.2)
Otherwise, if index() == rhs.index(), calls swap(*GET*<i>(*this), *GET*<i>(rhs)) where i is index()[.](#3.2.sentence-1)
- [(3.3)](#3.3)
Otherwise, exchanges values of rhs and *this[.](#3.3.sentence-1)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6426)
*Throws*: If index() == rhs.index(),
any exception thrown by swap(*GET*<i>(*this), *GET*<i>(rhs)) with i being index()[.](#4.sentence-1)
Otherwise, any exception thrown by the move constructor
of Ti or Tj with i being index() and j being rhs.index()[.](#4.sentence-2)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6435)
*Remarks*: If an exception is thrown during the call to function swap(*GET*<i>(*this), *GET*<i>(rhs)),
the states of the contained values of *this and of rhs are
determined by the exception safety guarantee of swap for lvalues ofTi with i being index()[.](#5.sentence-1)
If an exception is thrown during the exchange of the values of *this and rhs, the states of the values of *this and of rhs are determined by the exception safety guarantee of variant's move constructor[.](#5.sentence-2)
The exception specification is equivalent to the logical and ofis_nothrow_move_constructible_v<Ti> && is_nothrow_swappable_v<Ti> for all i[.](#5.sentence-3)