8.0 KiB
[expected.object.assign]
22 General utilities library [utilities]
22.8 Expected objects [expected]
22.8.6 Class template expected [expected.expected]
22.8.6.4 Assignment [expected.object.assign]
This subclause makes use of the following exposition-only function template:template<class T, class U, class... Args>constexpr void reinit-expected(T& newval, U& oldval, Args&&... args) { // exposition onlyif constexpr (is_nothrow_constructible_v<T, Args...>) { destroy_at(addressof(oldval)); construct_at(addressof(newval), std::forward(args)...); } else if constexpr (is_nothrow_move_constructible_v) { T tmp(std::forward(args)...); destroy_at(addressof(oldval)); construct_at(addressof(newval), std::move(tmp)); } else { U tmp(std::move(oldval)); destroy_at(addressof(oldval)); try { construct_at(addressof(newval), std::forward(args)...); } catch (...) { construct_at(addressof(oldval), std::move(tmp)); throw; }}}
constexpr expected& operator=(const expected& rhs);
Effects:
-
If this->has_value() && rhs.has_value() is true, equivalent to val = *rhs.
-
Otherwise, if this->has_value() is true, equivalent to:reinit-expected(unex, val, rhs.error())
-
Otherwise, if rhs.has_value() is true, equivalent to:reinit-expected(val, unex, *rhs)
-
Otherwise, equivalent to unex = rhs.error().
Then, if no exception was thrown, equivalent to: has_val = rhs.has_value(); return *this;
Returns: *this.
Remarks: This operator is defined as deleted unless:
is_copy_assignable_v is true and
is_copy_constructible_v is true and
is_copy_assignable_v is true and
is_copy_constructible_v is true and
is_nothrow_move_constructible_v || is_nothrow_move_constructible_v is true.
constexpr expected& operator=(expected&& rhs) noexcept(see below);
Constraints:
is_move_constructible_v is true and
is_move_assignable_v is true and
is_move_constructible_v is true and
is_move_assignable_v is true and
is_nothrow_move_constructible_v || is_nothrow_move_constructible_v is true.
Effects:
-
If this->has_value() && rhs.has_value() is true, equivalent to val = std::move(*rhs).
-
Otherwise, if this->has_value() is true, equivalent to:reinit-expected(unex, val, std::move(rhs.error()))
-
Otherwise, if rhs.has_value() is true, equivalent to:reinit-expected(val, unex, std::move(*rhs))
-
Otherwise, equivalent to unex = std::move(rhs.error()).
Then, if no exception was thrown, equivalent to: has_val = rhs.has_value(); return *this;
Returns: *this.
Remarks: The exception specification is equivalent to:is_nothrow_move_assignable_v && is_nothrow_move_constructible_v && is_nothrow_move_assignable_v && is_nothrow_move_constructible_v
template<class U = remove_cv_t<T>> constexpr expected& operator=(U&& v);
Constraints:
is_same_v<expected, remove_cvref_t> is false; and
remove_cvref_t is not a specialization of unexpected; and
is_constructible_v<T, U> is true; and
is_assignable_v<T&, U> is true; and
is_nothrow_constructible_v<T, U> || is_nothrow_move_constructible_v ||
is_nothrow_move_constructible_v is true.
Effects:
If has_value() is true, equivalent to: val = std::forward(v);
Otherwise, equivalent to:reinit-expected(val, unex, std::forward(v));has_val = true;
Returns: *this.
template<class G> constexpr expected& operator=(const unexpected<G>& e); template<class G> constexpr expected& operator=(unexpected<G>&& e);
Let GF be const G& for the first overload andG for the second overload.
Constraints:
is_constructible_v<E, GF> is true; and
is_assignable_v<E&, GF> is true; and
is_nothrow_constructible_v<E, GF> || is_nothrow_move_constructible_v ||
is_nothrow_move_constructible_v is true.
Effects:
If has_value() is true, equivalent to:reinit-expected(unex, val, std::forward(e.error()));has_val = false;
Otherwise, equivalent to:unex = std::forward(e.error());
Returns: *this.
template<class... Args> constexpr T& emplace(Args&&... args) noexcept;
Constraints: is_nothrow_constructible_v<T, Args...> is true.
Effects: Equivalent to:if (has_value()) { destroy_at(addressof(val));} else { destroy_at(addressof(unex)); has_val = true;}return *construct_at(addressof(val), std::forward(args)...);
template<class U, class... Args> constexpr T& emplace(initializer_list<U> il, Args&&... args) noexcept;
Constraints: is_nothrow_constructible_v<T, initializer_list&, Args...> is true.
Effects: Equivalent to:if (has_value()) { destroy_at(addressof(val));} else { destroy_at(addressof(unex)); has_val = true;}return *construct_at(addressof(val), il, std::forward(args)...);