Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::pair<T1,T2>::swap

      From cppreference.com
      <cpp‎ |utility‎ |pair
       
       
      Utilities library
       
       
      (1)
      void swap( pair& other)noexcept(/* see below */);
      (since C++11)
      (until C++20)
      constexprvoid swap( pair& other)noexcept(/* see below */);
      (since C++20)
      constexprvoid swap(const pair& other)constnoexcept(/* see below */);
      (2)(since C++23)

      Swapsfirst withother.first andsecond withother.second, as if byusingstd::swap; swap(first, other.first); swap(second, other.second);.

      If either selectedswap function call is ill-formed or does not swap the value of the member, the behavior is undefined.

      (until C++23)
      1) The program is ill-formed if eitherstd::is_swappable_v<T1> orstd::is_swappable_v<T2> is nottrue.
      2) The program is ill-formed if eitherstd::is_swappable_v<const T1> orstd::is_swappable_v<const T2> is nottrue.

      If either selectedswap function call does not swap the value of the member, the behavior is undefined.

      (since C++23)

      Contents

      [edit]Parameters

      other - pair of values to swap

      [edit]Return value

      (none)

      [edit]Exceptions

      noexcept specification:  
      noexcept(

           noexcept(swap(first, other.first))&&
           noexcept(swap(second, other.second))

      )

      In the expression above, the identifierswap is looked up in the same manner as the one used by the C++17std::is_nothrow_swappable trait.

      (until C++17)
      1)
      noexcept specification:  
      noexcept(

           std::is_nothrow_swappable_v<first_type>&&
           std::is_nothrow_swappable_v<second_type>

      )
      2)
      noexcept specification:  
      noexcept(

           std::is_nothrow_swappable_v<const first_type>&&
           std::is_nothrow_swappable_v<const second_type>

      )
      (since C++17)

      [edit]Example

      Run this code
      #include <iostream>#include <utility>#include <string>int main(){std::pair<int,std::string> p1(10,"test"), p2;    p2.swap(p1);std::cout<<"("<< p2.first<<", "<< p2.second<<")\n"; #if __cpp_lib_ranges_zip >= 202110L// Using the C++23 const qualified swap overload// (swap is no longer propagating pair constness)int i1=10, i2{};std::string s1("test"), s2;conststd::pair<int&,std::string&> r1(i1, s1), r2(i2, s2);    r2.swap(r1);std::cout<<"("<< i2<<", "<< s2<<")\n";#endif}

      Possible output:

      (10, test)(10, test)

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2456C++11thenoexcept specification is ill-formedmade to work

      [edit]See also

      swaps the values of two objects
      (function template)[edit]
      swaps the contents of twotuples
      (public member function ofstd::tuple<Types...>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/pair/swap&oldid=140835"

      [8]ページ先頭

      ©2009-2025 Movatter.jp