std::variant::swap

From cppreference.com
< cpp‎ | utility‎ | variant
 
 
 
 
void swap(variant& rhs)
(1) (since C++17)

Swaps two variant objects

  • if both *this and rhs are valueless by exception, does nothing
  • otherwise, if both *this and rhs hold the same alternative, calls swap(std::get<i>(*this), std:get<i>(rhs)) where i is index(). If an exception is thrown, the state of the values depends on the exception safety of the swap function called.
  • otherwise, exchanges values of rhs and *this. If an exception is thrown, the state of *this and rhs depends on exception safety of variant's move constructor and move assignment operators.

This overload only participates in overload resolution if std::is_swappable_v<T_i> is true for all T_i in Types...

Contents

[edit] Parameters

rhs - a variant object to swap with

[edit] Return value

(none)

[edit] Exceptions

May throw any exception thrown by swap(get<i>(*this), get<i>(rhs)) with i being index() and variant's move constructor and move assignment operator.

noexcept specification:  
noexcept((std::is_nothrow_move_constructible_v<Types> && std::is_nothrow_swappable_v<Types>) && ...))

[edit] Example