Files
cppdraft_translate/cppdraft/variant/assign.md
2025-10-25 03:02:53 +03:00

7.1 KiB
Raw Blame History

[variant.assign]

22 General utilities library [utilities]

22.6 Variants [variant]

22.6.3 Class template variant [variant.variant]

22.6.3.4 Assignment [variant.assign]

🔗

constexpr variant& operator=(const variant& rhs);

1

#

Let j be rhs.index().

2

#

Effects:

  • (2.1)

    If neither *this nor rhs holds a value, there is no effect.

  • (2.2)

    Otherwise, if *this holds a value but rhs does not, destroys the value contained in *this and sets *this to not hold a value.

  • (2.3)

    Otherwise, if index() == j, assigns the value contained in rhs to the value contained in *this.

  • (2.4)

    Otherwise, if either is_nothrow_copy_constructible_v is true oris_nothrow_move_constructible_v is false, equivalent to emplace(GET(rhs)).

  • (2.5)

    Otherwise, equivalent to operator=(variant(rhs)).

3

#

Postconditions: index() == rhs.index().

4

#

Returns: *this.

5

#

Remarks: This operator is defined as deleted unlessis_copy_constructible_v &&is_copy_assignable_v is true for all i.

If is_trivially_copy_constructible_v &&is_trivially_copy_assignable_v &&is_trivially_destructible_v is true for all i, this assignment operator is trivial.

🔗

constexpr variant& operator=(variant&& rhs) noexcept(see below);

6

#

Let j be rhs.index().

7

#

Constraints: is_move_constructible_v &&is_move_assignable_v istrue for all i.

8

#

Effects:

  • (8.1)

    If neither *this nor rhs holds a value, there is no effect.

  • (8.2)

    Otherwise, if *this holds a value but rhs does not, destroys the value contained in *this and sets *this to not hold a value.

  • (8.3)

    Otherwise, if index() == j, assigns GET(std::move(rhs)) to the value contained in *this.

  • (8.4)

    Otherwise, equivalent to emplace(GET(std::move(rhs))).

9

#

Returns: *this.

10

#

Remarks: If is_trivially_move_constructible_v &&is_trivially_move_assignable_v &&is_trivially_destructible_v is true for all i, this assignment operator is trivial.

The exception specification is equivalent tois_nothrow_move_constructible_v && is_nothrow_move_assignable_v for all i.

  • (10.1)

    If an exception is thrown during the call to Tj's move construction (with j being rhs.index()), the variant will hold no value.

  • (10.2)

    If an exception is thrown during the call to Tj's move assignment, the state of the contained value is as defined by the exception safety guarantee of Tj's move assignment; index() will be j.

🔗

template<class T> constexpr variant& operator=(T&& t) noexcept(see below);

11

#

Let Tj be a type that is determined as follows: build an imaginary function FUN(Ti) for each alternative type Ti for which Ti x[] = {std::forward(t)}; is well-formed for some invented variable x.

The overload FUN(Tj) selected by overload resolution for the expression FUN(std::forward(t)) defines the alternative Tj which is the type of the contained value after assignment.

12

#

Constraints:

is_same_v<remove_cvref_t, variant> is false,

is_assignable_v<Tj&, T> && is_constructible_v<Tj, T> is true, and

the expression FUN(std::forward(t)) (with FUN being the above-mentioned set of imaginary functions) is well-formed. [Note 1: variant<string, string> v; v = "abc"; is ill-formed, as both alternative types have an equally viable constructor for the argument. — end note]

13

#

Effects:

  • (13.1)

    If *this holds a Tj, assigns std::forward(t) to the value contained in *this.

  • (13.2)

    Otherwise, if is_nothrow_constructible_v<Tj, T> ||!is_nothrow_move_constructible_v is true, equivalent to emplace(std::forward(t)).

  • (13.3)

    Otherwise, equivalent to emplace(Tj(std::forward(t))).

14

#

Postconditions: holds_alternative(*this) is true, with Tj selected by the imaginary function overload resolution described above.

15

#

Returns: *this.

16

#

Remarks: The exception specification is equivalent to:is_nothrow_assignable_v<Tj&, T> && is_nothrow_constructible_v<Tj, T>

  • (16.1)

    If an exception is thrown during the assignment of std::forward(t) to the value contained in *this, the state of the contained value andt are as defined by the exception safety guarantee of the assignment expression; valueless_by_exception() will be false.

  • (16.2)

    If an exception is thrown during the initialization of the contained value, the variant object is permitted to not hold a value.