Daily bit(e) of C++ | correctly calling std::swap
Daily bit(e) of C++ ♻️62, Correctly calling std::swap for user defined types to avoid issues with ADL.
When using std::swap with user-defined types, we need to be careful with the consequences of Argument Dependent Lookup.
A qualified call to std::swap will always invoke the default implementation. An unqualified call can find a user-defined implementation using ADL but will not see the default one.
Therefore, to properly call std::swap, we have to first pull the default implementation into the local scope and only then make an unqualified call.
Alternatively, with C++20, a qualified call to std::ranges::swap will always do the correct thing.