STL classes define swap() method as void swap(A&), taking an l-value reference. See for example std::vector::swap, or the question Is it necessary to use std::move when swapping two objects?
Such definition means we cannot swap with r-values, since r-value won't bind to However, I see no harm in swapping with r-values. Construct it, steal from it, place some guts in it, destroy it. Done. We can add another overload void swap(A&&) to make it happen.
I see only one reason why we do not have this overload out of the box. Because instead of writing
v.swap(rvalue);
It is better to write
v = rvalue;
And instead of swapping we will trigger move-assignment, which is even more efficient. Am I right that this reason is valid? Is this the only reason?
rvalue.swap(v)?rvalue.swap(v), why I cannot write the opposite?swapoperation is commutative, so I'd expect it to be accessible in both ways.