table of contents
        
      
      
    - Tumbleweed 2024.07.05-1.3
 - Leap-16.0
 - Leap-15.6
 
| std::swap(std::variant)(3) | C++ Standard Libary | std::swap(std::variant)(3) | 
NAME¶
std::swap(std::variant) - std::swap(std::variant)
Synopsis¶
 Defined in header <variant>
  
   template <class... Types>
  
   (since C++17)
  
   void swap( std::variant<Types...>& lhs, (until C++20)
  
   std::variant<Types...>& rhs ) noexcept(/* see below */);
  
   template <class... Types>
  
   constexpr void swap( std::variant<Types...>& lhs, (since
    C++20)
  
   std::variant<Types...>& rhs ) noexcept(/* see
  
   below */);
  
   Overloads the std::swap algorithm for std::variant. Effectively calls
    lhs.swap(rhs).
  
   This overload participates in overload resolution only if
  
   std::is_move_constructible_v<T_i> and std::is_swappable_v<T_i>
    are both true for all
  
   T_i in Types...
Parameters¶
lhs, rhs - variant objects whose values to swap
Return value¶
(none)
Exceptions¶
 noexcept specification:
  
   noexcept(noexcept(lhs.swap(rhs)))
Example¶
// Run this code
  
   #include <variant>
  
   #include <string>
  
   #include <iostream>
  
   auto print = [](auto const& v, char term = '\n') {
  
   std::visit([](auto&& o) { std::cout << o; }, v);
  
   std::cout << term;
  
   };
  
   int main()
  
   {
  
   std::variant<int, std::string> v1{123}, v2{"XYZ"};
  
   print(v1, ' ');
  
   print(v2);
  
   std::swap(v1, v2);
  
   print(v1, ' ');
  
   print(v2);
  
   std::variant<double, std::string> v3{3.14};
  
   // std::swap(v1, v3); // ERROR: ~ inconsistent parameter packs
  
   }
Output:¶
 123 XYZ
  
   XYZ 123
  
   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 swap was not constexpr while the required made constexpr
  
   operations can be constexpr in C++20
See also¶
 swap swaps with another variant
  
   (public member function)
Category:¶
* conditionally noexcept
| 2024.06.10 | http://cppreference.com |