名稱空間
變體
操作

std::ranges::swap

來自 cppreference.com
< cpp‎ | 工具
 
 
 
定義於標頭檔案 <concepts>
namespace ranges {

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

}
(C++20 起)
(定製點物件)
呼叫簽名 (Call signature)
template< class T, class U >
constexpr void ranges::swap( T&& t, U&& u ) noexcept(/* 見下文 */);
(C++20 起)

交換由 tu 引用的值。

ranges::swap(t, u)表示式上等價於

  1. (void)swap(t, u),如果 tu 具有類或列舉型別,且該表示式有效,其中過載決議在名稱空間 std::ranges 中執行,並新增候選 template<class T> void swap(T&, T&) = delete;
    • 如果過載決議選中的函式未交換由 tu 引用的值,則程式格式錯誤;無需診斷。
  2. 否則,如果 tu 是相等範圍(但可能不同元素型別)的左值陣列,且 ranges::swap(*t, *u) 是一個有效表示式,則為 (void)ranges::swap_ranges(t, u),除了 noexcept((void)ranges::swap_ranges(t, u)) 等於 noexcept(ranges::swap(*t, *u))
  3. 否則,一個交換 tu 所引用值的表示式,如果它們都是相同型別 V 的左值,並且 V 滿足 std::move_constructible<V>std::assignable_from<V&, V>
  4. 否則,ranges::swap(t, u) 格式錯誤,當 ranges::swap(t, u) 出現在模板例項化的直接語境中時,可能導致替換失敗

自定義點物件

名稱 ranges::swap 表示一個“自定義點物件 (customization point object)”,它是一個 const 函式物件,型別為字面型別semiregular類。為便於說明,其型別中 cv 不限定的版本表示為 __swap_fn

__swap_fn 的所有例項都相等。在相同引數上呼叫型別為 __swap_fn 的不同例項的效果是等價的,無論表示該例項的表示式是左值還是右值,以及是否為 const 限定(但是,volatile 限定的例項不要求可呼叫)。因此,ranges::swap 可以自由複製,並且其副本可以互換使用。

給定一組型別 Args...,如果 std::declval<Args>()... 滿足上述 ranges::swap 引數的要求,則 __swap_fn 遵循

否則,__swap_fn 的函式呼叫運算子不參與過載決議。

[編輯] 示例

#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); // 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);
}

輸出

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

[編輯] 另見

指定型別可以被交換,或者兩種型別可以互相交換
(概念) [編輯]
交換兩個物件的值
(函式模板) [編輯]