Why don't STL containers simply implement move using swap?
Swapping is (usually) slower than optimally implemented move construction or at best (rarely) equally fast.
Swapping + eventual destruction is (usually) slower than optimally implemented move assignment + eventual destruction or at best (rarely) equally fast.
Swapping alone is indeed faster than move assignment because the destruction of elements is postponed. But such postponement of destruction of elements by the move assignment operator of a container is not allowed by the C++ standard, so the move assignment that you suggest is non-conforming. Besides, those elements typically will eventually be destroyed and when that happens, you would've only lost efficiency.
Standard implementers would be allowed to use swap as long as they destroyed the elements some other way. But there is no efficiency advantage in doing that, and that may be their reason if they don't use it.
P.S. The generic std::swap
- which is used when there is no member swap
- is implemented by performing three moves. Standard containers implement a member swap
that is faster than the generic swap, but still not as fast as a single move when including the eventual destruction.