std::expected<T,E>::swap

From cppreference.com
< cpp‎ | utility‎ | expected
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (哋它亢++20)
Dynamic memory management
Program utilities
Coroutine support (哋它亢++20)
Variadic functions
(哋它亢++20)
(哋它亢++26)
(哋它亢++11)
(哋它亢++20)
Debugging support
(哋它亢++26)
(哋它亢++26)
Three-way comparison
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)
General utilities
Date and time
Function objects
Formatting library (哋它亢++20)
(哋它亢++11)
Relational operators (deprecated in 哋它亢++20)
Integer comparison functions
(哋它亢++20)(哋它亢++20)(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
Swap and type operations
(哋它亢++20)
(哋它亢++14)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
Common vocabulary types
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++23)
Elementary string conversions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)


 
 
constexpr void swap( expected& other ) noexcept(/*see below*/);
(since 哋它亢++23)

Swaps the contents with those of other.

  • If T is (possibly cv-qualified) void, no effects.
  • Otherwise, equivalent to using std::swap; swap(**this, *other);.
  • If both this->has_value() and other.has_value() are false, equivalent to
    using std::swap; swap(this->error(), other.error());.
  • If this->has_value() is false and other.has_value() is true, calls other.swap(*this).
  • If this->has_value() is true and other.has_value() is false,
  • If 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));
  • Otherwise, let val be the member that represents the expected value and unex be 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() is false, and other.has_value() is true.

This function participates in overload resolution only if

Parameters

other - the optional object to exchange the contents with

Return value

(none)

Exceptions

If T is (possibly cv-qualified) void,
noexcept specification:  
Otherwise,

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

See also

(哋它亢++23)
specializes the std::swap algorithm
(function)