std::ranges::swap

From cppreference.com
< cpp‎ | utility
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)
Swap and type operations
ranges::swap
(C++20)
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

Elementary string conversions
(C++17)
(C++17)
 
Defined in header <concepts>
inline namespace /* unspecified */ {

    inline constexpr /* unspecified */ swap = /* unspecified */;

}
(since C++20)
(customization point object)
Call signature
template< class T, class U >

    requires /* see below */

constexpr void swap(T&& t, U&& u) noexcept(/* see below */);

Exchanges the values referenced by t and u.

A call to ranges::swap is expression-equivalent to:

1) (void)swap(std::forward<T>(t), std::forward<U>(u)), if that expression is valid, where the overload resolution is performed with the following candidates:
If the function selected by overload resolution does not exchange the values referenced by t and u, the program is ill-formed; no diagnostic required.
2) Otherwise, (void)ranges::swap_ranges(t, u), if T and U are lvalue references to array types of equal extent (but possibly different element types) and ranges::swap(*t, *u) is a valid expression;
3) Otherwise, if T and U are both V& for some type V that meets the syntactic requirements of std::move_constructible<V> and std::assignable_from<V&, V>, exchanges the referenced values as if by V v{std::move(t)}; t = std::move(u); u = std::move(v);. If the semantic requirements of either concept are not satisfied, the program is ill-formed; no diagnostic required.
4) In all other cases, a call to ranges::swap is ill-formed, which can result in substitution failure when ranges::swap(t, u) appears in the immediate context of a template instantiation.

Expression-equivalent

Expression e is expression-equivalent to expression f, if e and f have the same effects, either are both potentially-throwing or are both not potentially-throwing (i.e. noexcept(e) == noexcept(f)), and either are both constant subexpressions or are both not constant subexpressions.

Customization point objects

The name ranges::swap denotes a customization point object, which is a const function object of a literal semiregular class type (denoted, for exposition purposes, as swap_ftor). All instances of swap_ftor are equal. Thus, ranges::swap can be copied freely and its copies can be used interchangeably.

Given a set of types Args..., if std::declval<Args>()... meet the requirements for arguments to ranges::swap above, swap_ftor will satisfy std::invocable<const swap_ftor&, Args...>. Otherwise, no function call operator of swap_ftor participates in overload resolution.

Example

#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); // error: types mismatch
 
    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: types mismatch
 
    // char f1[]{1,2,3};
    // int  f2[]{4,5,6};
    // std::ranges::swap(f1, f2); // error: types mismatch
 
    int g1{27}, g2{28};
    std::ranges::swap(g1, g2);
    print("g", g1, g2);
}

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 = 28, g2 = 27

See also

specifies that a type can be swapped or that two types can be swapped with each other
(concept)
swaps the values of two objects
(function template)