std::stack::swap(3)
πΈ 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
swapis looked up in the same manner as the one used by the C++17std::is_nothrow_swappabletrait. - 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: βthenoexceptspecification is ill-formedβ; Correct behavior: βmade to workβ. Slay, it got fixed π₯.
π See also π
std::swap(std::stack)specializes thestd::swapalgorithm β 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
- 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