constexpr void swap( expected& other ) noexcept(/*see below*/); | (since C++23) |
Swaps the contents with those of other.
this->has_value() and other.has_value() are true: T is (possibly cv-qualified) void, no effects. using std::swap; swap(*this, other);. this->has_value() and other.has_value() are false, equivalent tousing std::swap; swap(this->error(), other.error());. this->has_value() is false and other.has_value() is true, calls other.swap(*this). this->has_value() is true and other.has_value() is false, T is (possibly cv-qualified) void, let unex be the member that represents the unexpected value, equivalent to: std::construct_at(std::addressof(unex), std::move(other.unex)); std::destroy_at(std::addressof(other.unex));
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;
}
}this->has_value() is false, and other.has_value() is true. This function participates in overload resolution only if.
T is (possibly cv-qualified) void, or std::is_swappable_v<T> is true, and std::is_swappable_v<E> is true, and T is (possibly cv-qualified) void, or std::is_move_constructible_v<T> is true, and std::is_move_constructible_v<E> is true, and T is (possibly cv-qualified) void std::is_nothrow_move_constructible_v<T> std::is_nothrow_move_constructible_v<E> | other | - | the optional object to exchange the contents with |
(none).
T is (possibly cv-qualified) void, noexcept specification: noexcept(
std::is_nothrow_move_constructible_v<E> && std::is_nothrow_swappable_v<E>
)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.
|
(C++23) | specializes the std::swap algorithm (function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/utility/expected/swap