std::optional::swap(3)
π NAME π
std::optional::swap - std::optional::swap (yes bestie, the name is the name, she said what she said)
Okay so welcome to the std::optional::swap page from the C++ Standard Libary ππ β and yes, thatβs exactly how the manual spells βLibary,β and I am not fixing it because character-for-character energy is the whole vibe.
β¨ Synopsis β¨
void swap( optional& other ) noexcept(/* see below */); (since C++17)
(constexpr since C++20)
Swaps the contents with those of other. Literally the swapiest swap, no cap π
- If neither
*thisnorothercontain a value, the function has no effect. Nothing happens. Iconic minimalism. - If only one of
*thisandothercontains a value (letβs call this objectinand the otherun), the contained value ofunis direct-initialized fromstd::move(*in), followed by destruction of the contained value ofinas if byin->T::~T(). After this call,indoes not contain a value;uncontains a value. - If both
*thisandothercontain values, the contained values are exchanged by callingusing std::swap; swap(**this, *other).Tlvalues must satisfySwappable.
π¨ The program is ill-formed if std::is_move_constructible_v<T> is false. Thatβs not a vibe check you can fail, bestie β itβs just ill-formed.
πΈ Parameters πΈ
otherβ the optional object to exchange the contents with π«Ά
π Return value π
(none) β she gives you nothing because the swap is the moment, periodt.
π¦ Exceptions π¦
noexcept specification:
noexcept(std::is_nothrow_move_constructible_v<T> &&
std::is_nothrow_swappable_v<T>)
In the case of thrown exception, the states of the contained values of *this and other are determined by the exception safety guarantees of swap of type T or Tβs move constructor, whichever is called. For both *this and other, if the object contained a value, it is left containing a value, and the other way round.
π Example π
// Run this code π
#include <iostream>
#include <optional>
#include <string>
int main()
{
std::optional<std::string> opt1("First example text");
std::optional<std::string> opt2("2nd text");
enum Swap { Before, After };
auto print_opts = [&](Swap e) {
std::cout << (e == Before ? "Before swap:\n" : "After swap:\n");
std::cout << "opt1 contains '" << opt1.value_or("") << "'\n";
std::cout << "opt2 contains '" << opt2.value_or("") << "'\n";
std::cout << (e == Before ? "---SWAP---\n": "\n");
};
print_opts(Before);
opt1.swap(opt2);
print_opts(After);
// Swap with only 1 set
opt1 = "Lorem ipsum dolor sit amet, consectetur tincidunt.";
opt2.reset();
print_opts(Before);
opt1.swap(opt2);
print_opts(After);
}
Output:
Before swap:
opt1 contains 'First example text'
opt2 contains '2nd text'
---SWAP---
After swap:
opt1 contains '2nd text'
opt2 contains 'First example text'
Before swap:
opt1 contains 'Lorem ipsum dolor sit amet, consectetur tincidunt.'
opt2 contains ''
---SWAP---
After swap:
opt1 contains ''
opt2 contains 'Lorem ipsum dolor sit amet, consectetur tincidunt.'
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
Columns: DR, Applied to, Behavior as published, Correct behavior.
P2231R1β applied toC++20; behavior as published:swap was not constexpr while the required operations can be constexpr in C++20; correct behavior:made constexpr.
π See also π
std::swap(std::optional)specializes thestd::swapalgorithm (C++17) (function template)
β Category β
- conditionally noexcept
This cutie was served fresh from http://cppreference.com on 2024.06.10, and the page handle is std::optional::swap(3) πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms