We have seen how swap functionality is implemented by the standard library, and what the expectations are for a swap implementation. Let's now see how to correctly support swap for your own types.
How to implement and use swap correctly
Implementing swap
We have seen that all STL containers, and many other standard library types (for example, std::thread), provide a swap() member function. While not required, it is the easiest way to implement swap that needs access to the private data of the class, and also the only way to swap an object with a temporary object of the same type. The proper way to declare the swap() member function is like this:
class C {
public:
void swap(C& rhs) noexcept;
};
Of course, the...