std::swap(std::array)(3)

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

πŸ’– NAME πŸ’–

std::swap(std::array) β€” std::swap(std::array), literally. Bestie, this is page std::swap(std::array)(3) from the C++ Standard Libary manual (yes, that spelling, it’s iconic), and we’re about to make swapping arrays make sense. No cap. 🫢


✨ Synopsis ✨

          Defined in header <array>
          template< class T, std::size_t N >
                                                     (since C++11)
          void swap( std::array<T, N>& lhs,          (until C++17)

                     std::array<T, N>& rhs );
          template< class T, std::size_t N >

          void swap( std::array<T, N>& lhs,          (since C++17)
                     std::array<T, N>& rhs )         (constexpr since C++20)

                         noexcept(/* see below */);

Okay so this overload specializes the std::swap algorithm for std::array. It swaps the contents of lhs and rhs, and it does that by calling lhs.swap(rhs). Slay. πŸ’…

This overload participates in overload resolution only if N == 0 or β€” since C++17 β€” std::is_swappable_v<T> is true. That’s the vibe check for whether it even shows up. πŸ”₯


🌸 Parameters 🌸

  • lhs, rhs β€” the containers whose contents you’re swapping, bestie. That’s it. That’s the whole parameter moment. πŸŽ€

πŸŽ€ Return value πŸŽ€

(none) β€” literally no return value. It swaps and dips. 🫢


πŸ¦‹ Complexity πŸ¦‹

Linear in size of the container. Highkey grows with how many elements are in there. 🧚


πŸ’œ Exceptions πŸ’œ

The noexcept specification is, since C++17:

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

So yes, it’s giving β€œI might be noexcept, depending on what lhs.swap(rhs) is doing.” Conditionally iconic. ✨


πŸ”₯ Example πŸ”₯

       // Run this code

        #include <algorithm>
        #include <iostream>
        #include <array>

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

            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';
        }

Output:

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

They really swapped. Alice got Bob’s numbers, Bob got Alice’s numbers, and the group chat is shook. πŸ˜­πŸ‘‘


πŸ‘€ See also πŸ‘€

  • swap β€” swaps the contents (public member function). Bestie, it’s the member-function bestie of this whole situation. 🌈

🏷️ Category 🏷️

  • conditionally noexcept β€” it’s giving conditional confidence. πŸ’€

Alright bestie, signing off with the official cute footer energy: source/version line is http://cppreference.com, date is 2024.06.10, and the page is std::swap(std::array)(3). Mwah. πŸ’‹πŸŽ€

πŸ“¦ where this page comes from