For a class in a namespace the proper way to provide swap()
is to define a free function in that namespace (e.g. How to overload std::swap()). But what is the best practice for a class that's in the global namespace?
cppreference has an example with a free swap()
function in the global namespace.
But this is the only place I've seen that done and I worry this was just an oversight.
In general, no library should use the global namespace, only the main application. And even that rarely, who knows when you will make part of it its own library?
Aside from that, the global scope ::
is a namespace like any other, thus if you define a type there, the associated free functions belong there too.
Otherwise, argument-dependent lookup cannot find them.