std::queue::swap(3)

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

✨ NAME ✨

bestie, welcome to std::queue::swap(3) from the C++ Standard Libary ✨ β€” the manual name is literally std::queue::swap(3), and the NAME line is exactly std::queue::swap - std::queue::swap πŸ’…. It's giving "swap your queue's contents" main character energy, no cap.


πŸ¦‹ Synopsis

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

Exchanges the contents of the container adaptor with those of other. Effectively calls:

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

πŸŽ€ Parameters

  • other β€” container adaptor to exchange the contents with πŸ’–

🌸 Return value

(none) β€” literally nothing, bestie 🫢


πŸ”₯ Exceptions

  • noexcept specification, (since C++11) and (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.

  • noexcept specification, (since C++17):
noexcept(std::is_nothrow_swappable_v<Container>)

πŸ¦‹ Complexity

Same as underlying container (typically constant) ✨


πŸ’œ Notes

Some implementations (e.g. libc++) provide the swap member function as an extension to pre-C++11 modes πŸ‘‘


πŸŽ€ Example

okay so // Run this code β€” here's the whole example, bestie:

#include <iostream>
#include <concepts>
#include <queue>
#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::queue s1(std::move(v1));
    std::queue 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 😭:

  • 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::queue) specializes the std::swap algorithm β€” (C++11) (function template) ⭐

πŸ‘‘ Category

  • conditionally noexcept

okay bestie, that's the whole page πŸ’– β€” source/version: http://cppreference.com, dated 2024.06.10 🌸

πŸ“¦ where this page comes from