std::tuple::swap(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ 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, and constexpr since 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 selected swap function 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 selected swap function 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.

  1. (since C++17) noexcept specification: noexcept((std::is_nothrow_swappable_v<Types> && ...))
  2. noexcept specification: 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 the std::swap algorithm (C++11) (function template)
  • swap swaps the contents (C++11) (public member function of std::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