I wasn't thinking of the trivial implementation. Because of exactly the drawbacks you describe.
operator=() should never be used for a swap operation. Why use deep copies if, after the function call, you still end up with exactly the same dynamic data? A shallow copy only moves the pointers around (and the primitive types). For the swap function this should be enough. And I'd be surprised if the standard C++ implementation did it differently.
Of course, this is only true for reasonable implementations of operator=(). But one can't assume that operator=() is going to be used in swap(). All that is required after the swap is that X behaves as Y did and Y behaves as X did.





Reply With Quote
Bookmarks