Files
2025-10-25 03:02:53 +03:00

8.0 KiB
Raw Permalink Blame History

[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]

1

#

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);

2

#

Effects:

  • (2.1)

    If this->has_value() && rhs.has_value() is true, equivalent to val = *rhs.

  • (2.2)

    Otherwise, if this->has_value() is true, equivalent to:reinit-expected(unex, val, rhs.error())

  • (2.3)

    Otherwise, if rhs.has_value() is true, equivalent to:reinit-expected(val, unex, *rhs)

  • (2.4)

    Otherwise, equivalent to unex = rhs.error().

Then, if no exception was thrown, equivalent to: has_val = rhs.has_value(); return *this;

3

#

Returns: *this.

4

#

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);

5

#

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.

6

#

Effects:

  • (6.1)

    If this->has_value() && rhs.has_value() is true, equivalent to val = std::move(*rhs).

  • (6.2)

    Otherwise, if this->has_value() is true, equivalent to:reinit-expected(unex, val, std::move(rhs.error()))

  • (6.3)

    Otherwise, if rhs.has_value() is true, equivalent to:reinit-expected(val, unex, std::move(*rhs))

  • (6.4)

    Otherwise, equivalent to unex = std::move(rhs.error()).

Then, if no exception was thrown, equivalent to: has_val = rhs.has_value(); return *this;

7

#

Returns: *this.

8

#

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);

9

#

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.

10

#

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;

11

#

Returns: *this.

🔗

template<class G> constexpr expected& operator=(const unexpected<G>& e); template<class G> constexpr expected& operator=(unexpected<G>&& e);

12

#

Let GF be const G& for the first overload andG for the second overload.

13

#

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.

14

#

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());

15

#

Returns: *this.

🔗

template<class... Args> constexpr T& emplace(Args&&... args) noexcept;

16

#

Constraints: is_nothrow_constructible_v<T, Args...> is true.

17

#

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;

18

#

Constraints: is_nothrow_constructible_v<T, initializer_list&, Args...> is true.

19

#

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)...);