std::expected<T,E>::operator= - cppreference.com (original) (raw)
| Primary template | ||
|---|---|---|
| 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 = std::remove_cv_t<T> > constexpr expected& operator=( U&& v ); | (3) | (since C++23) |
| template< class G > constexpr expected& operator=( const std::unexpected<G>& e ); | (4) | (since C++23) |
| template< class G > constexpr expected& operator=( std::unexpected<G>&& e ); | (5) | (since C++23) |
| void partial specialization | ||
| constexpr expected& operator=( const expected& other ); | (6) | (since C++23) |
| constexpr expected& operator=( expected&& other ) noexcept(/* see below */); | (7) | (since C++23) |
| template< class G > constexpr expected& operator=( const std::unexpected<G>& e ); | (8) | (since C++23) |
| template< class G > constexpr expected& operator=( std::unexpected<G>&& e ); | (9) | (since C++23) |
| Helper function template | ||
| template< class T, class U, class... Args > constexpr void /*reinit-expected*/( T& newval, U& oldval, Args&&... args ) | (10) | (since C++23) (exposition only*) |
Assigns a new value to an existing expected object.
Contents
- 1 Parameters
- 2 Effects
- 3 Return value
- 4 Constraints and supplement information
- 5 Exceptions
- 6 Example
- 7 Defect reports
- 8 See also
[edit] Parameters
| other | - | another expected object whose contained value to assign |
|---|---|---|
| v | - | value to assign to the contained value |
| e | - | std::unexpected object whose contained value to assign |
| newval | - | the contained value to be constructed |
| oldval | - | the contained value to be destroyed |
| args | - | the arguments used as initializers of newval |
[edit] Effects
[edit] Primary template assignment operators
1,2) Assigns the state of other to *this.
If has_value() and rhs.has_value() have different values (i.e. one of *this and other contains an expected value val and the other contains an unexpected value unex ), the exposition-only function template reinit-expected is called to safely update the state.
- The contained value is assigned as follows:
| Value of has_value() | Value of other.has_value() | |
|---|---|---|
| true | false | |
| true | val = *other; | reinit-expected (unex, val, other.error()); |
| false | reinit-expected (val, unex, *other); | unex = other.error(); |
- The contained value is assigned as follows:
| Value of has_value() | Value of other.has_value() | |
|---|---|---|
| true | false | |
| true | val = std::move(*other); | reinit-expected (unex, val, std::move(other.error())); |
| false | reinit-expected (val, unex, std::move(*other)); | unex = std::move(other.error()); |
Then, if no exception was thrown, executes has_val = other.has_value();.
- The expected value is assigned as follows:
| Value of has_value() | Equivalent to |
|---|---|
| true | val = std::forward<U>(v); |
| false | reinit-expected(val, unex, std::forward<U>(v));has_val = false; |
4,5) The unexpected value is assigned as follows:
| Overload | Value of has_value() | Equivalent to |
|---|---|---|
| (4) | true | reinit-expected(val, unex, std::forward<const G&>(e.error()));has_val = false; |
| false | unex = std::forward<const G&>(e.error()); | |
| (5) | true | reinit-expected(val, unex, std::forward<G>(e.error()));has_val = false; |
| false | unex = std::forward<G>(e.error()); |
[edit] void partial specialization assignment operators
[edit] Helper function template
The exposition-only function template _reinit-expected_ is “defined” as follows:
This function template is called when the assignment is going to make *this hold the alternative value (i.e. from expected value to unexpected value, or from unexpected value to expected value).
In this case, the old value oldval needs to be destroyed before constructing the new value newval. However, the construction of newval may throw an exception. In order to provide strong exception safety guarantee, the old value needs to be restored before rethrowing the exception so that *this will have a valid state while the exception is being handled.
[edit] Return value
1-9) *this
[edit] Constraints and supplement information
[edit] Primary template assignment operators
- This overload participates in overload resolution only if all following conditions are satisfied:
- std::is_same_v<expected, std::remove_cvref_t<U>> is false.
- std::remove_cvref_t<U> is not a specialization of
std::unexpected. - All following values are true:
[edit] void partial specialization assignment operators
[edit] Exceptions
[edit] Example
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3886 | C++23 | the default template argument of overload (3) was T | changed to std::remove_cv_t<T> |
| LWG 4025 | C++23 | overload (7) was defined as deleted if E is notmove constructible or not move assignable | it does not participate inoverload resolution in this case |
[edit] See also
| | constructs the expected value in-place (public member function) [edit] | | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |