std::swap(std::pair)(3)

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

πŸ’… std::swap(std::pair) β€” yeah bestie, we're swapping pairs today

okay so this is the std::swap page for std::pair, straight out of the C++ Standard Library. it lives in <utility> and it does exactly one thing extremely well: swaps two pairs. no cap. 🫢


✨ NAME ✨

std::swap(std::pair) β€” literally std::swap(std::pair), that's the whole name, she's not complicated πŸ’…


🌸 SYNOPSIS 🌸

Defined in header <utility>

template< class T1, class T2 >
void swap( std::pair<T1,T2>& x, std::pair<T1,T2>& y )
    noexcept(/* see below */);

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

template< class T1, class T2 >
constexpr void swap( std::pair<T1,T2>& x, std::pair<T1,T2>& y )
    noexcept(/* see below */);

(1) (since C++20)

template< class T1, class T2 >
constexpr void swap( const std::pair<T1,T2>& x, const std::pair<T1,T2>& y )
    noexcept(/* see below */);

(2) (since C++23)


πŸ¦‹ DESCRIPTION πŸ¦‹

Swaps the contents of x and y. That's it. It's equivalent to x.swap(y). iconic behavior, honestly. ✨

  • 1) this overload only participates in overload resolution if std::is_swappable_v<first_type> && std::is_swappable_v<second_type> is true. (since C++17)
  • 2) this overload only participates in overload resolution if std::is_swappable_v<const first_type> && std::is_swappable_v<const second_type> is true.
🧚 friendly reminder that those two "only participates if…" clauses are literally part of the spec β€” she's picky about overload resolution and honestly? valid.

πŸŽ€ PARAMETERS πŸŽ€

  • x, y β€” the pairs whose contents to swap 🫢

🌈 RETURN VALUE 🌈

(none) β€” she gives you nothing back, she just does the thing. main character energy tbh.


πŸ”₯ EXCEPTIONS πŸ”₯

the noexcept specification is:

noexcept(noexcept(x.swap(y)))

⭐ EXAMPLE ⭐

// Run this code

#include <iostream>
#include <utility>

int main()
{
    auto p1 = std::make_pair(10, 3.14);
    auto p2 = std::pair(12, 1.23); // CTAD, since C++17

    auto print_p1_p2 = [&](auto msg) {
        std::cout << msg
                  << "p1 = {" << std::get<0>(p1)
                  << ", "     << std::get<1>(p1) << "}, "
                  << "p2 = {" << std::get<0>(p2)
                  << ", "     << std::get<1>(p2) << "}\n";
    };

    print_p1_p2("Before p1.swap(p2): ");
    p1.swap(p2);
    print_p1_p2("After  p1.swap(p2): ");
    std::swap(p1, p2);
    print_p1_p2("After swap(p1, p2): ");
}

πŸ’– Output πŸ’–

Before p1.swap(p2): p1 = {10, 3.14}, p2 = {12, 1.23}
After  p1.swap(p2): p1 = {12, 1.23}, p2 = {10, 3.14}
After swap(p1, p2): p1 = {10, 3.14}, p2 = {12, 1.23}

like… she swapped twice and ended up right back where she started. we love a round trip πŸ˜­πŸ’€


πŸ‘‘ SEE ALSO πŸ‘‘

  • swap β€” swaps the values of two objects (function template)
  • std::swap(std::tuple) (C++11) β€” specializes the std::swap algorithm (function template)

πŸ“ Category πŸ“

  • * conditionally noexcept

aaaaaaaaand that's the page!! πŸ¦‹ go forth and swap your pairs, bestie, you're gonna slay. no notes. πŸ’œ

source: cppreference.com β€” version 2024.06.10 β€” mwah, kisses, see u next page πŸŽ€βœ¨

πŸ“¦ where this page comes from