As proper use of std::swap is:
using std::swap;
swap(a,b);
It is a bit verbose but it makes sure that if a,b have better swap defined it gets picked.
So now my question is why std::swap is not implemented using this technique so user code would just need to call std::swap?
So something like this (ignoring noexcept and constraints for brevity):
namespace std {
namespace internal {
template <class T> // normal swap implementation
void swap(T& a, T& b) { // not intended to be called directly
T tmp = std::move(a);
a = std::move(b);
b = std::move(tmp);
}
}
template <class T>
void swap(T& a, T& b) {
using internal::swap;
swap(a,b);
}
}
This is getting into tautology territory, but it doesn't do it that way because that's not its purpose.
The purpose of
std::swapis to be the swap function of last resort. It's not supposed to be the thing you call directly, unless what you really want is to use the swap-of-last-resort.Now, you can argue that what you suggest is a better paradigm for customization points. Hindsight is always 20/20; not everything that STL did was the right idea (see
vector<bool>).As for why we can't change that now, that's a different matter.
std::swapis the swap function of last resort. So it is theoretically possible that people are calling it and expecting it to bypass any user-defined swap code. And therefore, changing it in this way breaks their code.