std::swap(s...rward_list)(3)

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

πŸ’… std::swap(std::forward_list) β€” the C++ Standard Library glow-up, section 3 πŸ¦‹

Okay so bestie, we're in the C++ Standard Libary manual, section 3, and today's main character is std::swap(std::forward_list). Yes, the title is literally std::swap(std::forward_list) twice, she said "I contain multitudes." Periodt.


✨ NAME ✨

std::swap(std::forward_list) β€” which, and I cannot stress this enough, is std::swap(std::forward_list). Iconic. No notes. πŸŽ€


🌸 SYNOPSIS 🌸

She lives in the header <forward_list> (say it with me: Defined in header <forward_list>), and here's her whole lewk:

Defined in header <forward_list>

template< class T, class Alloc >
void swap( std::forward_list<T, Alloc>& lhs,        (since C++11)
           std::forward_list<T, Alloc>& rhs );      (until C++17)

template< class T, class Alloc >
void swap( std::forward_list<T, Alloc>& lhs,        (since C++17)
           std::forward_list<T, Alloc>& rhs )
             noexcept(/* see below */);

So like β€” (since C++11) she's been here, and (until C++17) that first form was the vibe, then (since C++17) she got the noexcept(/* see below */) glow-up. Growth. We love to see it. πŸ’–


🫢 DESCRIPTION 🫢

So what does she do? She Specializes the std::swap algorithm for std::forward_list. That's the whole assignment, lowkey.

Then: Swaps the contents of lhs and rhs. Simple. Elegant. Two lists walk in, two lists walk out with each other's stuff.

And the actual mechanics? Calls lhs.swap(rhs). That's it. She's not doing the heavy lifting herself, she's delegating β€” main character energy with good management skills, honestly. πŸ‘‘


🌈 PARAMETERS 🌈

  • lhs, rhs β€” the containers whose contents to swap. Two besties trading outfits. That's the whole bit. πŸ“

⭐ RETURN VALUE ⭐

(none)

Nothing. Nada. She gives you the swap and dips. No cap, she's just that generous. πŸ’€


🧚 COMPLEXITY 🧚

Constant.

Constant!! Not linear, not "depends," just constant. O(1) behavior from an icon who respects your time. We stan an efficient queen. ⚑


πŸ”₯ EXCEPTIONS πŸ”₯

Here's the noexcept specification: β€” and this is (since C++17), so mark your calendars:

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

Gorgeous. She's only noexcept when lhs.swap(rhs) is noexcept. Conditional. Situational. She reads the room. πŸ™ˆ


πŸ’œ EXAMPLE πŸ’œ

// Run this code β€” okay bestie, here's the full thing, typed out exactly, don't @ me:

#include <algorithm>
#include <iostream>
#include <forward_list>

int main()
{
    std::forward_list<int> alice{1, 2, 3};
    std::forward_list<int> bob{7, 8, 9, 10};

    auto print = [](const int& n) { std::cout << ' ' << n; };

    // Print state before swap
    std::cout << "Alice:";
    std::for_each(alice.begin(), alice.end(), print);
    std::cout << "\n" "Bob  :";
    std::for_each(bob.begin(), bob.end(), print);
    std::cout << '\n';

    std::cout << "-- SWAP\n";
    std::swap(alice, bob);

    // Print state after swap
    std::cout << "Alice:";
    std::for_each(alice.begin(), alice.end(), print);
    std::cout << "\n" "Bob  :";
    std::for_each(bob.begin(), bob.end(), print);
    std::cout << '\n';
}

Alice and Bob. The classic duo. Not me getting emotionally invested in two std::forward_list<int>s. 😭

πŸ¦‹ Output πŸ¦‹

Alice: 1 2 3
Bob  : 7 8 9 10
-- SWAP
Alice: 7 8 9 10
Bob  : 1 2 3

THEY SWAPPED. Look at them. Alice took Bob's 7 8 9 10 and Bob got the 1 2 3 and everybody's thriving. The way that -- SWAP just sits there being a whole diva divider. Ate. πŸ”₯


πŸ‘‘ SEE ALSO πŸ‘‘

  • swap β€” swaps the contents. Yeah. That's the description. (public member function). She's the member-function version and she also just... swaps the contents. Two queens, same energy, different scopes. Slay. πŸ’…

πŸŽ€ CATEGORY πŸŽ€

  • * conditionally noexcept

Which is exactly the vibe we established in EXCEPTIONS. Consistent. Reliable. A little bit conditional. Like all of us. 🧚


Ngl bestie, this one's genuinely simple: you call std::swap(alice, bob), it calls lhs.swap(rhs), both are constant time, and the noexcept-ness rides entirely on whatever lhs.swap(rhs) says. No hidden lore, no traps. Just two lists getting a whole new wardrobe. πŸ’–

Fr fr, this page came from cppreference.com, dated 2024.06.10, and it's std::swap(s...rward_list)(3) β€” go forth and swap, bestie. 🫢✨

πŸ“¦ where this page comes from