std::swap(std::tuple)(3)

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

πŸŽ€ std::swap(std::tuple) β€” C++ Standard Libary manual πŸŽ€

Okay so this is the std::swap(std::tuple)(3) page from the C++ Standard Libary manual (yes, the header spells it β€œLibary,” and we are respecting the icon’s typo πŸ˜­πŸ’…). It’s the tuple-swap situation, and it’s giving β€œsmall function, big main character energy.” Let’s go, bestie. ✨


🌸 NAME 🌸

std::swap(std::tuple) β€” std::swap(std::tuple)

Yes, the name is literally the name. We love a self-aware queen. πŸ‘‘

πŸ“œ SYNOPSIS πŸ“œ

Defined in header <tuple>:

template< class... Types >
void swap( std::tuple<Types...>& lhs,
           std::tuple<Types...>& rhs ) noexcept(/* see below */);

(since C++11) (until C++20)

template< class... Types >
constexpr void swap( std::tuple<Types...>& lhs,
                     std::tuple<Types...>& rhs ) noexcept(/* see below */);

(1) (since C++20)

template< class... Types >
constexpr void swap( const std::tuple<Types...>& lhs,
                     const std::tuple<Types...>& rhs ) noexcept(/* see below */);

(2) (since C++23)

Swaps the contents of lhs and rhs. Equivalent to lhs.swap(rhs). Periodt. πŸ’–

  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)

🧸 PARAMETERS 🧸

  • lhs, rhs β€” tuples whose contents to swap.

🎁 RETURN VALUE 🎁

(none) β€” literally nothing, bestie. It’s a return-nothing situation. 🫢


🚨 EXCEPTIONS 🚨

noexcept specification:

noexcept(noexcept(lhs.swap(rhs)))

That’s the whole spec, no cap. πŸ”₯


🌈 EXAMPLE 🌈

Run this code, bestie:

// 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}

πŸ‘€ SEE ALSO πŸ‘€

  • swap β€” swaps the contents of two tuples (public member function).
  • std::swap(std::pair) β€” specializes the std::swap algorithm (C++11) (function template).

🏷️ CATEGORY 🏷️

  • * conditionally noexcept

Okay bestie, that’s the whole page! Source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::swap(std::tuple)(3). Bye and slay responsibly πŸ’–πŸŒˆβœ¨

πŸ“¦ where this page comes from