std::variant<Types...>::swap
From cppreference.com
 
 
 
 
 
 C++ 
 Feature test macros (C++20)
 Concepts library (C++20)
 Metaprogramming library (C++11)
 Ranges library (C++20)
 Filesystem library (C++17)
 Concurrency support library (C++11)
 Execution control library (C++26)
Utilities library 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
Relational operators (deprecated in C++20) Integer comparison functions  
 
 
 
 Swap and type operations  Common vocabulary types 
 Type support (basic types, RTTI)
 Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
 Coroutine support (C++20)
 Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
  (C++20)(C++20)(C++20)
  (C++20)(C++20)(C++20)
 General utilities
(C++20)(C++20)(C++20)
  (C++20)(C++20)(C++20)
(C++20)
std::variant 
 
 
variant::swap
(C++26)
void swap( variant& rhs ) noexcept(/* see below */);
 
 (since C++17) (constexpr since C++20)
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_if <i>(this), *std::get_if <i>(std::addressof (rhs))) where i is index(). If an exception is thrown, the state of the values depends on the exception safety of theswapfunction 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.
The program is ill-formed unless type T_i are Swappable and std::is_move_constructible_v <T_i> is true for all T_i in Types....
[edit] Parameters
 rhs
 -
 a 
variant object to swap with
[edit] Return value
(none)
[edit] Exceptions
If this->index() == rhs.index(), may throw any exception thrown by swap(*std::get_if <i>(this), *std::get_if <i>(std::addressof (rhs))) with i being index().
Otherwise, may throw any exception thrown by the move constructors of the alternatives currently held by *this and rhs.
noexcept specification:  
noexcept(((std::is_nothrow_move_constructible_v <Types> &&
std::is_nothrow_swappable_v <Types>) && ...))
std::is_nothrow_swappable_v <Types>) && ...))
[edit] Notes
| Feature-test macro | Value | Std | Feature | 
|---|---|---|---|
| __cpp_lib_variant | 202106L | (C++20) (DR) | Fully constexpr std::variant | 
[edit] Example
Run this code
#include <iostream> #include <string> #include <variant> int main() { std::variant <int, std::string > v1{2}, v2{"abc"}; std::visit ([](auto&& x) { std::cout << x << ' '; }, v1); std::visit ([](auto&& x) { std::cout << x << '\n'; }, v2); v1.swap(v2); std::visit ([](auto&& x) { std::cout << x << ' '; }, v1); std::visit ([](auto&& x) { std::cout << x << '\n'; }, v2); }
Output:
2 abc abc 2
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior | 
|---|---|---|---|
| P2231R1 | C++20 | swapwas not constexpr while non-trivial destructors can be constexpr in C++20 | made constexpr |