In the standard library, if a class type has a specialized swap algorithm, then it will have a member function swap
and a free function swap
that simpl
The free function cannot see the private guts of the class, so the actual functionality needs to be provided as a class member. However, you want the free function because it's a customization point (using ADL); so a generic algorithm will call the free swap
in order to achieve whatever swapping is appropriate for the type at hand.
The member function is also useful for constructions like T().swap(existing_thing)
, since the free function requires lvalue arguments.
I suppose the alternative would have been to make swap
a public friend function declared inside the class, which could be found via ADL just as well.