C++ named requirements: Swappable
Min standard notice:
Any lvalue or rvalue of this type can be swapped with any lvalue or rvalue of some other type, using unqualified function call swap() in the context where both std::swap and the user-defined swap()s are visible.
# Notes
It is unspecified whether
# Example
#include <iostream>
#include <vector>
struct IntVector
{
std::vector<int> v;
IntVector& operator=(IntVector) = delete; // not assignable
void swap(IntVector& other)
{
v.swap(other.v);
}
void operator()(auto rem, auto term = " ")
{
std::cout << rem << "{{";
for (int n{}; int e : v)
std::cout << (n++ ? ", " : "") << e;
std::cout << "}}" << term;
}
};
void swap(IntVector& v1, IntVector& v2)
{
v1.swap(v2);
}
int main()
{
IntVector v1{{1, 1, 1, 1}}, v2{{2222, 2222}};
auto prn = [&]{ v1("v1", ", "), v2("v2", ";\n"); };
// std::swap(v1, v2); // Compiler error! std::swap requires MoveAssignable
prn();
std::iter_swap(&v1, &v2); // OK: library calls unqualified swap()
prn();
std::ranges::swap(v1, v2); // OK: library calls unqualified swap()
prn();
}
# Defect reports
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 226 | C++98 | it was unclear how the standard library uses swap | clarified to use both std:: and ADL-found swap |