Namespaces
Variants
Views
Actions

std::expected<T,E>::operator=

From cppreference.com
< cpp‎ | utility‎ | expected
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
 
constexpr expected& operator=( const expected& other );
(1) (since C++23)
constexpr expected& operator=( expected&& other ) noexcept(/*see below*/);
(2) (since C++23)
template< class U = T >
constexpr expected& operator=( U&& v );
(3) (since C++23)
(T is not cv void)
template< class G >
constexpr expected& operator=( const unexpected<G>& other );
(4) (since C++23)
template< class G >
constexpr expected& operator=( unexpected<G>&& other );
(5) (since C++23)

Assigns a new value to an existing expected object.

1,2) Assigns the state of other.
  • If this->has_value() and other.has_value() are both true:
    • If T is (possibly cv-qualified) void, no effects.
    • Otherwise, assigns the expected value contained in other to the value contained in *this, as if by **this = *other (1) or **this = std::move(*other) (2).
  • Otherwise, if this->has_value() is true:
    • If T is (possibly cv-qualified) void, constructs the unexpected value contained in *this from other.error() (1) or std::move(other.error()) (2).
    • Otherwise, destroys the value contained in *this, constructs an unexpected value from other.error() (1) or std::move(other.error()) (2), and makes *this contain the newly constructed value. If an exception is thrown, the old value is retained; *this does not become valueless.
  • Otherwise, if other.has_value() is true:
    • if T is (possibly cv-qualified) void, destroys the value contained in *this;
    • otherwise, destroys the value contained in *this, constructs an expected value from *other (1) or std::move(*other) (2), and makes *this contain the newly constructed value. If an exception is thrown, the old value is retained; *this does not become valueless.
  • Otherwise, assigns the unexpected value contained in other to the value contained in *this, as if by this->error() = other.error() (1) or this->error() = std::move(other.error()) (2).

If no exception was thrown, after assignment, has_value() is equal to other.has_value().

3) Assigns from expected value.
  • If this->has_value() is true, equivalent to **this = v.
  • Otherwise, destroys the value contained in *this, constructs an expected value from v, and makes *this contain the newly constructed value. If an exception is thrown, the old value is retained; *this does not become valueless.

If no exception was thrown, after assignment, this->has_value() is true.

4,5) Assigns from unexpected value.

Let GF be const G& for overload (4), and G for overload (5).

  • If this->has_value() is true, destroys the value contained in *this, constructs an unexpected value from std::forward<GF>(e.error()), and makes *this contain the newly constructed value. If an exception is thrown, the old value is retained; *this does not become valueless.
  • Otherwise, equivalent to this->error() = std::forward<GF>(e.error()).

If no exception was thrown, after assignment, this->has_value() is false.


In all cases, if T is not (possibly cv-qualified) void, the destruction of old value and construction of new value is performed as if by the following exposition-only function reinit_expected.

template< class NewType, class OldType, class... Args >
constexpr void reinit_expected( NewType& new_val, OldType& old_val, Args&&... args ) {
    if constexpr (std::is_nothrow_constructible_v<NewType, Args...>) {
        std::destroy_at(std::addressof(old_val));
        std::construct_at(std::addressof(new_val), std::forward<Args>(args)...);
    } else if constexpr (std::is_nothrow_move_constructible_v<NewType>) {
        NewType temp(std::forward<Args>(args)...);
        std::destroy_at(std::addressof(old_val));
        std::construct_at(std::addressof(new_val), std::move(temp));
    } else {
        OldType temp(std::move(old_val));
        std::destroy_at(std::addressof(old_val));
        try {
            std::construct_at(std::addressof(new_val), std::forward<Args>(args)...);
        } catch (...) {
            std::construct_at(std::addressof(old_val), std::move(temp));
            throw;
        }
    }
}

Contents

[edit] Parameters

other - another expected object whose contained value to assign
value - value to assign to the contained value
e - std::unexpected object whose contained value to assign

[edit] Return value

*this

[edit] Exceptions

1) Throws any exception thrown by the copy constructor or copy assignment operator of T or E.
2) If T is (possibly cv-qualified) void, Otherwise,
3) Throws any exception thrown by the constructor or assignment operator of T.
4,5) Throws any exception thrown by the constructor or assignment operator of E.

[edit] Example

[edit] See also

(C++23)
constructs the expected value in-place
(public member function) [edit]