Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::swap

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
       
      Defined in header<concepts>
      namespace ranges{

         inlinenamespace/* unspecified */{
             inlineconstexpr/* unspecified */ swap=/* unspecified */;
         }

      }
      (since C++20)
      (customization point object)
      Call signature
      template<class T,class U>
      constexprvoidranges::swap( T&& t, U&& u)noexcept(/* see below */);
      (since C++20)

      Exchanges the values referenced byt andu.

      ranges::swap(t, u) isexpression-equivalent to:

      1. (void)swap(t, u), ift oru has class or enumeration type, and that expression is valid, where theoverload resolution is performed within namespacestd::ranges with the additional candidatetemplate<class T>void swap(T&, T&)= delete;.
        • If the function selected by overload resolution does not exchange the values referenced byt andu, the program is ill-formed; no diagnostic required.
      2. Otherwise,(void)ranges::swap_ranges(t, u), ift andu are lvalue arrays of equal extent (but possibly different element types) andranges::swap(*t,*u) is a valid expression, except thatnoexcept((void)ranges::swap_ranges(t, u)) is equal tonoexcept(ranges::swap(*t,*u)).
      3. Otherwise, an expression which exchanges the referenced values oft andu, if they are both lvalues of the same typeV that modelsstd::move_constructible<V> andstd::assignable_from<V&, V>.
      4. Otherwise,ranges::swap(t, u) is ill-formed, which can result insubstitution failure whenranges::swap(t, u) appears in the immediate context of a template instantiation.

      Customization point objects

      The nameranges::swap denotes acustomization point object, which is a constfunction object of aliteralsemiregular class type. SeeCustomizationPointObject for details.

      [edit]Example

      Run this code
      #include <array>#include <concepts>#include <iostream>#include <ranges>#include <string_view>#include <vector> void print(std::string_view name,            std::ranges::common_rangeautoconst& p,            std::ranges::common_rangeautoconst& q){std::cout<< name<<"1{ ";for(autoconst& i: p)std::cout<< i<<' ';std::cout<<"}, "<< name<<"2{ ";for(autoconst& i: q)std::cout<< i<<' ';std::cout<<"}\n";} void print(std::string_view name,int p,int q){std::cout<< name<<"1 = "<< p<<", "<< name<<"2 = "<< q<<'\n';} struct IntLike{int v;}; void swap(IntLike& lhs,int& rhs){std::swap(lhs.v, rhs);} void swap(int& lhs, IntLike& rhs){std::swap(lhs, rhs.v);} std::ostream& operator<<(std::ostream& out, IntLike i){return out<< i.v;} int main(){std::vector a1{10,11,12}, a2{13,14};    std::ranges::swap(a1, a2);    print("a", a1, a2); std::array b1{15,16,17}, b2{18,19,20};    std::ranges::swap(b1, b2);    print("b", b1, b2); // std::array c1{1, 2, 3}; std::array c2{4, 5};// std::ranges::swap(c1, c2); // error: no swap found by ADL int d1[]{21,22,23}, d2[]{24,25,26};    std::ranges::swap(d1, d2);    print("d", d1, d2); // int e1[]{1, 2, 3}, e2[]{4, 5};// std::ranges::swap(e1, e2); // error: extents mismatch // char f1[]{1, 2, 3};// int  f2[]{4, 5, 6};// std::ranges::swap(f1, f2); // error: no swap(*f1, *f2) found by ADL     IntLike g1[]{1,2,3};int     g2[]{4,5,6};    std::ranges::swap(g1, g2);// heterogeneous swap supported    print("g", g1, g2); int h1{27}, h2{28};    std::ranges::swap(h1, h2);    print("h", h1, h2);}

      Output:

      a1{ 13 14 }, a2{ 10 11 12 }b1{ 18 19 20 }, b2{ 15 16 17 }d1{ 24 25 26 }, d2{ 21 22 23 }g1{ 4 5 6 }, g2{ 1 2 3 }h1 = 28, h2 = 27

      [edit]See also

      specifies that a type can be swapped or that two types can be swapped with each other
      (concept)[edit]
      swaps the values of two objects
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/ranges/swap&oldid=151405"

      [8]ページ先頭

      ©2009-2025 Movatter.jp