std::tuple<Types...>::swap

Header: <tuple>

Calls swap (which might be std::swap, or might be found via ADL) for each element in *this and its corresponding element in other.

# Declarations

void swap( tuple& other ) noexcept(/* see below */);

(since C++11) (constexpr since C++20)

constexpr void swap( const tuple& other ) noexcept(/* see below */) const;

(since C++23)

# Parameters

# Return value

(none)

# Example

#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): ");
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 2456C++11the noexcept specification is ill-formedmade to work

# See also