std::stack::swap(3)

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

🌸 NAME 🌸

okay bestie, this is the std::stack::swap page β€” manual name std::stack::swap(3), from the C++ Standard Libary (yes, the header says β€œLibary”, we are keeping that detail, no cap πŸ’€). The NAME line is literally std::stack::swap - std::stack::swap, which is iconic self-reference behavior.


✨ Synopsis ✨

void swap( stack& other ) noexcept(/* see below */);  (since C++11)

It exchanges the contents of the container adaptor with those of other.

It effectively calls:

using std::swap;
swap(c, other.c);

🧸 Parameters 🧸

  • other β€” container adaptor to exchange the contents with.

πŸ’œ Return value πŸ’œ

  • (none) β€” literally none, not a single return value, periodt πŸ’….

😭 Exceptions 😭

The noexcept specification has two eras, bestie:

  • Since C++11, until C++17: noexcept(noexcept(swap(c, other.c))).
  • 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.
  • Since C++17: noexcept(std::is_nothrow_swappable_v<Container>).

πŸ”₯ Complexity πŸ”₯

Same as underlying container (typically constant). That’s the whole vibe check.


πŸŽ€ Notes πŸŽ€

Some implementations (e.g. libc++) provide the swap member function as an extension to pre-C++11 modes. So yeah, old-mode land has a little bonus situation going on.


🧚 Example 🧚

The original page says // Run this code before the example, so here’s the code exactly:

#include <iostream>
#include <concepts>
#include <stack>
#include <string>
#include <string_view>
#include <vector>

template<typename Adaptor>
requires (std::ranges::input_range<typename Adaptor::container_type>)
void print(std::string_view name, const Adaptor& adaptor)
{
    struct Printer : Adaptor // to use protected Adaptor::Container c;
    {
        void print(std::string_view name) const
        {
            std::cout << name << " [" << std::size(this->c) << "]: ";
            for (auto const& elem : this->c)
                std::cout << elem << ' ';
            std::cout << '\n';
        }
    };

    static_cast<Printer const&>(adaptor).print(name);
}

int main()
{
    std::vector<std::string> v1{"1","2","3","4"},
                             v2{"β±―","B","Ζ†","D","Ǝ"};

    std::stack s1(std::move(v1));
    std::stack s2(std::move(v2));

    print("s1", s1);
    print("s2", s2);

    s1.swap(s2);

    print("s1", s1);
    print("s2", s2);
}

πŸ“ Output πŸ“

s1 [4]: 4 3 2 1
s2 [5]: Ǝ D Ζ† B β±―
s1 [5]: Ǝ D Ζ† B β±―
s2 [4]: 4 3 2 1

πŸ’€ Defect reports πŸ’€

The following behavior-changing defect reports were applied retroactively to previously published C++ standards:

  • DR LWG 2456 β€” applied to C++11; Behavior as published: β€œthe noexcept specification is ill-formed”; Correct behavior: β€œmade to work”. Slay, it got fixed πŸ”₯.

🌈 See also 🌈

  • std::swap(std::stack) specializes the std::swap algorithm β€” C++11, function template. That’s the function template right there πŸ‘‘.

⭐ Category ⭐

  • * conditionally noexcept β€” yup, that’s the category.

πŸ’– source: http://cppreference.com, version/date: 2024.06.10, page: std::stack::swap(3). bye bestie, go swap something safely πŸ’…βœ¨

πŸ“¦ where this page comes from