std::array::swap(3)
🎀 std::array::swap(3) — from the C++ Standard Libary, bestie 🎀
okay so we're in the group chat for <array>, and the main character is std::array::swap, no cap. This is the C++ Standard Libary page (yes, “Libary” exactly, don’t come for me), and we’re about to make swapping arrays make sense. 💅
✨ NAME ✨
std::array::swap — std::array::swap. That’s it, that’s the name, iconic recursive name energy 🫶.
✨ Synopsis ✨
void swap( array& other ) noexcept(/* see below */); (since C++11)
(constexpr since C++20)
This exchanges the contents of the container with those of other. It does not cause iterators and references to associate with the other container. Like, your it1 stays with its original array, fr fr. 🦋
✨ Parameters ✨
other— container to exchange the contents with. That’s the whole parameter list, bestie.
✨ Return value ✨
(none). Literally no return value, not even a little one 🫶.
✨ Exceptions ✨
Here’s the noexcept tea ☕, because this part is lowkey the whole vibe check:
- Until C++17, the
noexceptspecification is:
noexcept(noexcept(swap(std::declval<T&>(), std::declval<T&>())))
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, the
noexceptspecification is:
noexcept(std::is_nothrow_swappable_v<T>)
- For zero-sized arrays, the
noexceptspecification is:
noexcept
✨ Complexity ✨
Linear in size of the container. So it’s giving “walk the whole array” energy, bestie 💅.
✨ Example ✨
// Run this code
#include <array>
#include <iostream>
template<class Os, class V> Os& operator<<(Os& os, const V& v)
{
os << '{';
for (auto i : v)
os << ' ' << i;
return os << " } ";
}
int main()
{
std::array<int, 3> a1{1, 2, 3}, a2{4, 5, 6};
auto it1 = a1.begin();
auto it2 = a2.begin();
int& ref1 = a1[1];
int& ref2 = a2[1];
std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' '
<< ref2 << '\n';
a1.swap(a2);
std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' '
<< ref2 << '\n';
// Note that after swap iterators and references stay associated
// with their original array, e.g. `it1` still points to element a1[0],
// `ref1` still refers to a1[1].
}
🌸 Output 🌸
{ 1 2 3 } { 4 5 6 } 1 4 2 5
{ 4 5 6 } { 1 2 3 } 4 1 5 2
✨ Defect reports ✨
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. Basically the committee said “oops” and patched the vibe retroactively 👑.
- LWG 2456 — applied to C++11. Behavior as published:
the noexcept specification is ill-formed; correct behavior:made to work. Not me getting fixed retroactively 💀✨.
✨ See also ✨
std::swap(std::array)— specializes thestd::swapalgorithm (C++11) (function template). Periodt.
✨ Categories ✨
conditionally noexceptunconditionally noexcept
okay bye bestie, that’s the whole page 💖 source/version: http://cppreference.com · 2024.06.10 — footer vibes, slay 🎀
📦 where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 · MIT
- 💖 girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms