Sometimes classes are referencing other classes. Implementing std::swap() for such classes cannot be straightforward, because it would lead to swapping of origi
std::swap()
You may use std::reference_wrapper instead of direct reference (which you cannot swap) or pointer (which may be nullptr). Something like:
std::reference_wrapper
swap
nullptr
class A { std::reference_wrapper r; public: A(int& v) : r(v) {} void swap(A& rhs) { std::swap(r, rhs.r); } int& get() const { return r; } };
Live example