std::ranges::swap(3)
π std::ranges::swap β bestie edition π
okay so this is the manual page for std::ranges::swap from the C++ Standard Libary (yes, that spelling is literally in the header, no cap π). Itβs giving C++20 customization-point-object main character energy, and it lives in the <concepts> header. Letβs get into it β¨π
πΈ NAME πΈ
std::ranges::swap β literally std::ranges::swap (the name ate and left no crumbs π
).
β¨ Synopsis β¨
okay bestie, hereβs the setup. Defined in header <concepts>, and the namespace situation is this whole little nest πͺΊ:
Defined in header <concepts>
namespace ranges {
inline namespace /* unspecified */ {
inline constexpr /* unspecified */ swap = /* (since C++20)
unspecified */;
(customization point object)
}
}
π Call signature
template< class T, class U >
constexpr void ranges::swap( T&& t, U&& u ) (since C++20)
noexcept(/* see below */);
It exchanges the values referenced by t and u, periodt π«.
ranges::swap(t, u) is expression-equivalent to:
(void)swap(t, u), iftoruhas class or enumeration type, and that expression is valid, where the overload resolution is performed within namespacestd::rangeswith the additional candidatetemplate<class T> void swap(T&, T&) = delete;.- If the function selected by overload resolution does not exchange the values referenced by
tandu, the program is ill-formed; no diagnostic required. (yes bestie, βno diagnostic requiredβ is exactly as unhinged as it sounds ππ)
- If the function selected by overload resolution does not exchange the values referenced by
- Otherwise,
(void)ranges::swap_ranges(t, u), iftanduare 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)). - Otherwise, an expression which exchanges the referenced values of
tandu, if they are both lvalues of the same typeVthat modelsstd::move_constructible<V>andstd::assignable_from<V&, V>.- The result of applying the
noexceptoperator to that expression is equal tostd::is_nothrow_move_constructible_v<V> && std::is_nothrow_move_assignable_v<V>. - That expression is a constant expression if:
Vis aLiteralType,- both
t = std::move(u))andu = std::move(t)are constant subexpressions, and - the full-expressions of the initializers in the following declarations are constant subexpressions:
V v1(std::move(t));V v2(std::move(u));
- The result of applying the
- Otherwise,
ranges::swap(t, u)is ill-formed, which can result in substitution failure whenranges::swap(t, u)appears in the immediate context of a template instantiation.
π§ Customization point objects π§
The name ranges::swap denotes a customization point object, which is a const function object of a literal semiregular class type. For exposition purposes, the cv-unqualified version of its type is denoted as __swap_fn.
All instances of __swap_fn are equal. The effects of invoking different instances of type __swap_fn on the same arguments are equivalent, regardless of whether the expression denoting the instance is an lvalue or rvalue, and is const-qualified or not (however, a volatile-qualified instance is not required to be invocable). Thus, ranges::swap can be copied freely and its copies can be used interchangeably. slay, copyable icon behavior π
β¨
Given a set of types Args..., if std::declval<Args>()... meet the requirements for arguments to ranges::swap above, __swap_fn models:
std::invocable<__swap_fn, Args...>,std::invocable<const __swap_fn, Args...>,std::invocable<__swap_fn&, Args...>, andstd::invocable<const __swap_fn&, Args...>.
Otherwise, no function call operator of __swap_fn participates in overload resolution.
π 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_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);
}
π 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
π See also π
swappableβ(C++20)(concept)β specifies that a type can be swapped or that two types can be swapped with each other. The page also has the nameswappable_withright before thewith each otherbit, so Iβm keeping both visible bestie ππ«swappable_withβ(C++20)(concept)β the exact little fragment next to this name iswith each other.swapβ(function template)β swaps the values of two objects.
okay thatβs the whole page, bestie! source: http://cppreference.com, version/date: 2024.06.10, and this was the manual page for std::ranges::swap(3) π«Άπβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms