std::pair::swap(3)
π NAME π
std::pair::swap - std::pair::swap. This is the std::pair::swap(3) page, bestie, and the manual name folded in near the top is C++ Standard Libary β yes, literally spelled that way in the header, iconic, no notes.
β¨ Synopsis β¨
Okay so there are these overloads, and the vibe is very βread the standard, bestieβ:
void swap( pair& other ) noexcept(/* see below */);
(since C++11) (until C++20)
constexpr void swap( pair& other ) noexcept(/* see below */);
(1) (since C++20)
constexpr void swap( const pair& other ) const noexcept(/* see below */);
(2) (since C++23)
It swaps first with other.first and second with other.second, as if by:
using std::swap;
swap(first, other.first); swap(second, other.second);
(until C++23)If either selected swap function call is ill-formed or does not swap the value of the member, the behavior is undefined.1)The program is ill-formed if eitherstd::is_swappable_v<T1>orstd::is_swappable_v<T2>is not true.2)The program is ill-formed if eitherstd::is_swappable_v<const T1>orstd::is_swappable_v<const T2>is not true.(since C++23)If either selected swap function call does not swap the value of the member, the behavior is undefined.
πΈ Parameters πΈ
otherβ pair of values to swap.
π Return value π
(none) β literally nothing comes back, bestie.
π Exceptions π
noexcept specification, pre-C++17 edition (until C++17):
noexcept(
noexcept(swap(first, other.first)) &&
noexcept(swap(second, other.second))
)
In the expression above, the identifier swap is looked up in the same manner as the one used by the C++17 std::is_nothrow_swappable trait.
1) noexcept specification (since C++17):
noexcept(
std::is_nothrow_swappable_v<first_type> &&
std::is_nothrow_swappable_v<second_type>
)
2) noexcept specification:
noexcept(
std::is_nothrow_swappable_v<const first_type> &&
std::is_nothrow_swappable_v<const second_type>
)
π Example π
Run this code, bestie:
#include <iostream>
#include <utility>
#include <string>
int main()
{
std::pair<int, std::string> p1(10, "test"), p2;
p2.swap(p1);
std::cout << "(" << p2.first << ", " << p2.second << ")\n";
#if __cpp_lib_ranges_zip >= 202110L
// Using the C++23 const qualified swap overload
// (swap is no longer propagating pair constness)
int i1 = 10, i2{};
std::string s1("test"), s2;
const std::pair<int&, std::string&> r1(i1, s1), r2(i2, s2);
r2.swap(r1);
std::cout << "(" << i2 << ", " << s2 << ")\n";
#endif
}
π¦ Possible output: π¦
(10, test)
(10, test)
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR:
LWG 2456; Applied to: C++11; Behavior as published: thenoexceptspecification is ill-formed; Correct behavior: made to work.
π₯ See also π₯
swapβ swaps the values of two objects (function template).swapβ swaps the contents of two tuples (public member function ofstd::tuple<Types...>).
π Category π
conditionally noexcept
π«Ά Thatβs the whole page, bestie β source/version http://cppreference.com, dated 2024.06.10. π
π¦ 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