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

2.5 KiB

[variant.swap]

22 General utilities library [utilities]

22.6 Variants [variant]

22.6.3 Class template variant [variant.variant]

22.6.3.7 Swap [variant.swap]

🔗

constexpr void swap(variant& rhs) noexcept(see below);

1

#

Mandates: is_move_constructible_v is true for all i.

2

#

Preconditions: Each Ti meets the Cpp17Swappable requirements ([swappable.requirements]).

3

#

Effects:

  • (3.1)

    If valueless_by_exception() && rhs.valueless_by_exception() no effect.

  • (3.2)

    Otherwise, if index() == rhs.index(), calls swap(GET(*this), GET(rhs)) where i is index().

  • (3.3)

    Otherwise, exchanges values of rhs and *this.

4

#

Throws: If index() == rhs.index(), any exception thrown by swap(GET(*this), GET(rhs)) with i being index().

Otherwise, any exception thrown by the move constructor of Ti or Tj with i being index() and j being rhs.index().

5

#

Remarks: If an exception is thrown during the call to function swap(GET(*this), GET(rhs)), the states of the contained values of *this and of rhs are determined by the exception safety guarantee of swap for lvalues ofTi with i being index().

If an exception is thrown during the exchange of the values of *this and rhs, the states of the values of *this and of rhs are determined by the exception safety guarantee of variant's move constructor.

The exception specification is equivalent to the logical and ofis_nothrow_move_constructible_v && is_nothrow_swappable_v for all i.