std::expected::swap(3)
okay bestie, this is std::expected::swap(3) from the C++ Standard Libary (yes, thatβs the spelling on the page, not me π), and itβs literally the swap function for std::expected π
.
πΈ NAME πΈ
std::expected::swap - std::expected::swap β¨
β¨ Synopsis β¨
constexpr void swap( expected& other ) noexcept(/*see below*/);
(since C++23)
It swaps the contents with those of other β like, the whole vibe moves over. Hereβs the exact play-by-play:
- If both
this->has_value()andother.has_value()aretrue:- If
Tis (possibly cv-qualified)void, no effects. - Otherwise, equivalent to
using std::swap; swap(**this, *other);.
- If
- If both
this->has_value()andother.has_value()arefalse, equivalent tousing std::swap; swap(this->error(), other.error());. - If
this->has_value()isfalseandother.has_value()istrue, callsother.swap(*this). - If
this->has_value()istrueandother.has_value()isfalse:- If
Tis (possibly cv-qualified)void, letunexbe the member that represents the unexpected value, equivalent to:
- If
std::construct_at(std::addressof(unex), std::move(other.unex));
std::destroy_at(std::addressof(other.unex));
- Otherwise, let
valbe the member that represents the expected value andunexbe the member that represents the unexpected value, equivalent to:
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;
}
}
- In either case, if no exception was thrown, after swap,
this->has_value()isfalse, andother.has_value()istrue.
This function participates in overload resolution only if:
- either
Tis (possibly cv-qualified)void, orstd::is_swappable_v<T>istrue, and std::is_swappable_v<E>istrue, and- either
Tis (possibly cv-qualified)void, orstd::is_move_constructible_v<T>istrue, and std::is_move_constructible_v<E>istrue, and- at least one of the following is
true:Tis (possibly cv-qualified)voidstd::is_nothrow_move_constructible_v<T>std::is_nothrow_move_constructible_v<E>
π Parameters π
otherβ the optional object to exchange the contents with π
π Return value π
(none) β literally nothing comes back, bestie π«Ά
π₯ Exceptions π₯
If T is (possibly cv-qualified) void, noexcept specification:
noexcept(
std::is_nothrow_move_constructible_v<E> &&
std::is_nothrow_swappable_v<E>
)
Otherwise, noexcept specification:
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>
)
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 or T's and E's move constructor, whichever is called. For both *this and other, if the object contained an expected value, it is left containing an expected value, and the other way round.
π Example π
This section is incomplete.
Reason: no example
π See also π
swap(std::expected)specializes thestd::swapalgorithm β (C++23) (function) β¨
π Categories π
- conditionally noexcept
- Todo no example
thatβs the tea from http://cppreference.com, dated 2024.06.10 β mwah ππ«Ά
π¦ 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