std::swap(std::tuple)
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Relational operators (deprecated in C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
Common vocabulary types
std::tuple
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
swap(std::tuple)
(C++23)
(C++23)
(C++23)
Deduction guides (C++17)
Defined in header
<tuple>
(1)
template< class... Types >
(since C++11) void swap( std::tuple <Types...>& lhs,
(until C++20)
template< class... Types >
(since C++20)
constexpr void swap( std::tuple <Types...>& lhs,
template< class... Types >
(2)
(since C++23)
constexpr void swap( const std::tuple <Types...>& lhs,
Swaps the contents of lhs and rhs. Equivalent to lhs.swap(rhs).
1) This overload participates in overload resolution only if std::is_swappable_v <Ti> is true for all i from 0 to sizeof...(Types).
2) This overload participates in overload resolution only if std::is_swappable_v <const Ti> is true for all i from 0 to sizeof...(Types).
(since C++17)[edit] Parameters
lhs, rhs
-
tuples whose contents to swap
[edit] Return value
(none)
[edit] Exceptions
noexcept specification:
noexcept(noexcept(lhs.swap(rhs)))
[edit] Example
Run this code
#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}