std::tuple::swap(3)
β¨ NAME β¨
std::tuple::swap β yep, the name is literally std::tuple::swap, no notes. This is the std::tuple::swap(3) page from the C++ Standard Libary manual (that spelling is a whole vibe, weβre keeping it π
).
π Synopsis π
Defined in header <tuple>:
- (1)
void swap( tuple& other ) noexcept(/* see below */);β since C++11, andconstexprsince C++20. - (2)
constexpr void swap( const tuple& other ) noexcept(/* see below */) const;β since C++23.
Calls swap (which might be std::swap, or might be found via ADL) for each element in *this and its corresponding element in other.
π¨ Undefined behavior alert, bestie: (until C++23) if any selectedswapfunction call is ill-formed, or does not swap the corresponding elements of both tuples, the behavior is undefined. Then thereβs this sentence too: if any selectedswapfunction call does not swap the corresponding elements of both tuples, the behavior is undefined. So the swap has to actually swap the matching elements. No fake swapping. π
- (1) The program is ill-formed if
(std::is_swappable_v<Types> && ...)is not true. (since C++23) - (2) The program is ill-formed if
(std::is_swappable_v<const Types> && ...)is not true.
π Parameters π
otherβ tuple of values to swap.
π¦ Return value π¦
(none) β literally nothing comes back, bestie.
π¨ Exceptions π¨
Hereβs the pre-C++17 (until C++17) noexcept specification:
noexcept(
noexcept(swap(std::declval<T0&>>(), std::declval<T0&>())) &&
noexcept(swap(std::declval<T1&>>(), std::declval<T1&>())) &&
noexcept(swap(std::declval<T2&>>(), std::declval<T2&>())) &&
...
)
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.
- (since C++17)
noexceptspecification:noexcept((std::is_nothrow_swappable_v<Types> && ...)) noexceptspecification:noexcept((std::is_nothrow_swappable_v<const Types> && ...))
π§ͺ Example π§ͺ
// Run this code β hereβs the whole little demo:
#include <iostream>
#include <string>
#include <tuple>
int main()
{
std::tuple<int, std::string, float> p1{42, "ABCD", 2.71}, p2;
p2 = std::make_tuple(10, "1234", 3.14);
auto print_p1_p2 = [&](auto rem)
{
std::cout << rem
<< "p1 = {" << std::get<0>(p1)
<< ", " << std::get<1>(p1)
<< ", " << std::get<2>(p1) << "}, "
<< "p2 = {" << std::get<0>(p2)
<< ", " << std::get<1>(p2)
<< ", " << std::get<2>(p2) << "}\n";
};
print_p1_p2("Before p1.swap(p2): ");
p1.swap(p2);
print_p1_p2("After p1.swap(p2): ");
swap(p1, p2);
print_p1_p2("After swap(p1, p2): ");
}
π» Output π»
Before p1.swap(p2): p1 = {42, ABCD, 2.71}, p2 = {10, 1234, 3.14}
After p1.swap(p2): p1 = {10, 1234, 3.14}, p2 = {42, ABCD, 2.71}
After swap(p1, p2): p1 = {42, ABCD, 2.71}, p2 = {10, 1234, 3.14}
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 2456β applied to C++11; behavior as published:the noexcept specification is ill-formed; correct behavior:made to work.
π See also π
std::swap(std::tuple)specializes thestd::swapalgorithm (C++11) (function template)swapswaps the contents (C++11) (public member function ofstd::pair<T1,T2>)
π·οΈ Category π·οΈ
- conditionally noexcept
source/version: http://cppreference.com Β· date: 2024.06.10 β thatβs the footer, bestie, mwah π
π¦ 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