Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::swap(std::variant)

      From cppreference.com
      <cpp‎ |utility‎ |variant
       
       
      Utilities library
       
       
      Defined in header<variant>
      template<class...Types>

      void swap(std::variant<Types...>& lhs,

                 std::variant<Types...>& rhs)noexcept(/* see below */);
      (since C++17)
      (constexpr since C++20)

      Overloads thestd::swap algorithm forstd::variant. Effectively callslhs.swap(rhs).

      This overload participates in overload resolution only ifstd::is_move_constructible_v<T_i> andstd::is_swappable_v<T_i> are bothtrue for allT_i inTypes....

      Contents

      [edit]Parameters

      lhs, rhs -variant objects whose values to swap

      [edit]Return value

      (none)

      [edit]Exceptions

      noexcept specification:  
      noexcept(noexcept(lhs.swap(rhs)))

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_variant202106L(C++20)
      (DR)
      Fullyconstexprstd::variant

      [edit]Example

      Run this code
      #include <iostream>#include <string>#include <variant> void print(autoconst& 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 XYZXYZ 123

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      P2231R1C++20swap was notconstexpr while the required operations can beconstexpr in C++20madeconstexpr

      [edit]See also

      swaps with anothervariant
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant/swap2&oldid=172840"

      [8]ページ先頭

      ©2009-2025 Movatter.jp