
on Mon Sep 07 2009, Howard Hinnant <howard.hinnant-AT-gmail.com> wrote:
std::swap will only do a self-move-assignment using a moved-from object.
You mean the target will be a moved-from object. OK, so?
Of course vector will never use this definition. But if it did, then:
template <class _Tp, class _Allocator> inline vector<_Tp, _Allocator>& vector<_Tp, _Allocator>::operator=(vector&& __x) { clear(); swap(__x); return *this; }
would work just fine. clear() is a no-op, and then you swap an empty capacity vector with itself.
My point is that I don't want to be told I need to put a if(this != &x) in my move assignment operator. It is a useless performance penalty. Everyone should not have to pay (in terms of lost performance) for someone's bad code just to make sure self-move- assignment does a no-op instead of crashes.
And the reason this argument doesn't also apply to copy assignment is...?
If you want to cast an lvalue to an rvalue, that's fine. Just make sure your code logic is such that such a cast is safe. I believe std::swap's use of move assignment is safe because by the time it move-assigns, if it is self referencing, all of your resources have already been moved to a local temp.
Then what is the coding guideline, exactly? "Make sure it's safe" doesn't qualify. -- Dave Abrahams BoostPro Computing http://www.boostpro.com