std::ranges::swap
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   定义于头文件  <concepts>
  | 
||
|   inline namespace /* unspecified */ {     inline constexpr /* unspecified */ swap = /* unspecified */;  | 
 (C++20 起)  (定制点对象)  | 
|
|   调用签名  | 
||
|   template< class T, class U >     requires /* see below */  | 
||
交换 t 和 u 所引用的值。
调用 ranges::swap :
-  表达式等价于 (void)swap(std::forward<T>(t), std::forward<U>(u)) ,若以下列候选进行重载决议则该表达式合法:
- template<class T> void swap(T&, T&) = delete;
 - template<class T, std::size_t N> void swap(T(&)[N], T(&)[N]) = delete;
 -  实参依赖查找所找到的任何 
swap的声明。 
- 若重载决议所选择的函数不交换 
t和u所引用的值,则程序为谬构;不要求诊断。 
 -  否则表达式等价于 (void)ranges::swap_ranges(t, u) ,除了 noexcept(ranges::swap(t), ranges::swap(u)) 等于 noexcept(ranges::swap(*t, *u)) ,若 
T和U为到相等长度(但元素类型可能不同)的数组类型的左值引用,且 ranges::swap(*t, *u) 为合法表达式; -  否则如同用 V v(std::move(t)); t = std::move(u); u = std::move(v); 交换其被引用值,若 
T与U均为V&,而V是满足 std::move_constructible<V> 和 std::assignable_from<V&, V> 的某个类型,- 调用是否为潜在抛出由上述操作指定。
 -  若上述操作与以交换 
u与v进行的上述操作均可用于常量求值,则调用为常量子表达式。 
- 若任一概念未被实现,则程序为谬构;不要求诊断。
 
 -  所有其他情况下,对 
ranges::swap的调用为谬构,这能在 ranges::swap(t, u) 出现于模板实例化的立即语境时导致替换失败。 
表达式等价
表达式 e 表达式等价于表达式 f ,若 e 与 f 拥有相同效果,均为潜在抛出或均非潜在抛出(即 noexcept(e) == noexcept(f) ),且均为常量子表达式或均非常量子表达式。
定制点对象
名字 ranges::swap 代表一个定制点对象,它是字面 semiregular 类类型(为说明目的以 swap_ftor 表示)的 const 函数对象。swap_ftor 的所有实例均相等。从而能自由地复制 ranges::swap ,且能交替使用其副本。
给定类型集合 Args... ,若 std::declval<Args>()... 满足上面对于 ranges::swap 的参数要求,则 swap_ftor 将满足 std::invocable<const swap_ftor&, Args...> 。否则, swap_ftor 的函数调用运算符不参与重载决议。
示例
运行此代码
#include <array> #include <concepts> #include <iostream> #include <ranges> #include <string_view> #include <vector> void print(std::string_view const name, std::ranges::common_range auto const& p, std::ranges::common_range auto const& q) { std::cout << name << "1{ "; for (auto const& i : p) std::cout << i << ' '; std::cout << "}, " << name << "2{ "; for (auto const& i : q) std::cout << i << ' '; std::cout << "}\n"; } void print(std::string_view const name, int p, int q) { std::cout << name << "1 = " << p << ", " << name << "2 = " << q << '\n'; } 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); // 错误:类型不匹配 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); // 错误:类型不匹配 // char f1[]{1,2,3}; // int f2[]{4,5,6}; // std::ranges::swap(f1, f2); // 错误:类型不匹配 int g1{27}, g2{28}; std::ranges::swap(g1, g2); print("g", g1, g2); }
输出:
a1{ 13 14 }, a2{ 10 11 12 }
b1{ 18 19 20 }, b2{ 15 16 17 }
d1{ 24 25 26 }, d2{ 21 22 23 }
g1 = 28, g2 = 27参阅
|    (C++20)  | 
  指定一个类型能进行交换,或两个类型能彼此交换  (概念)  | 
|    交换两个对象的值   (函数模板)  |