Namespaces
Variants

std::ranges:: swap

From cppreference.net
Utilities library
定义于头文件 <concepts>
namespace ranges {

inline namespace /* 未指定 */ {
inline constexpr /* 未指定 */ swap = /* 未指定 */ ;
}

}
(C++20 起)
(定制点对象)
调用签名
template < class T, class U >
constexpr void ranges:: swap ( T && t, U && u ) noexcept ( /* 见下文 */ ) ;
(C++20 起)

交换 t u 所引用的值。

ranges:: swap ( t, u ) 表达式等价性 上等同于:

  1. ( void ) swap ( t, u ) ,若 t u 具有类或枚举类型,且该表达式有效,其中 重载决议 在命名空间 std::ranges 内执行,并附加候选函数 template < class T > void swap ( T & , T & ) = delete ;
    • 若重载决议选择的函数未交换 t u 所引用的值,则程序非良构;不要求诊断。
  2. 否则, ( void ) ranges:: swap_ranges ( t, u ) ,若 t u 是等长度的左值数组(但元素类型可能不同)且 ranges:: swap ( * t, * u ) 是有效表达式,但 noexcept ( ( void ) ranges:: swap_ranges ( t, u ) ) 等于 noexcept ( ranges:: swap ( * t, * u ) )
  3. 否则,交换 t u 所引用值的表达式,若它们均为满足 std:: move_constructible < V > std:: assignable_from < V & , V > 概念的同一类型 V 的左值。
  4. 否则, ranges:: swap ( t, u ) 非良构,当 ranges:: swap ( t, u ) 出现在模板实例化的直接上下文时,可能导致 替换失败

定制点对象

名称 ranges::swap 表示一个 定制点对象 ,这是一个常量 函数对象 ,其类型为 字面量 semiregular 类类型。详情请参阅 定制点对象

示例

#include <array>
#include <concepts>
#include <iostream>
#include <ranges>
#include <string_view>
#include <vector>
void print(std::string_view 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 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); // 错误:ADL未找到swap函数
    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); // 错误:ADL未找到swap(*f1, *f2)
    IntLike g1[]{1, 2, 3};
    int     g2[]{4, 5, 6};
    std::ranges::swap(g1, g2); // 支持异构交换
    print("g", g1, g2);
    int h1{27}, h2{28};
    std::ranges::swap(h1, h2);
    print("h", h1, h2);
}

输出:

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

参见

指定类型可交换或两种类型可相互交换
(概念)
交换两个对象的值
(函数模板)