c++ - Why STL classes do not overload swap() for rvalues? -


stl classes define swap() method void swap(a&), taking l-value reference. see example std::vector::swap, or question "is `std::move` necessary here?".

such definition means cannot swap r-values, since r-value won't bind however, see no harm in swapping r-values. construct it, steal it, place guts in it, destroy it. done. can add overload void swap(a&&) make happen.

i see 1 reason why not have overload out of box. because instead of writing

v.swap(rvalue); 

it better write

v = rvalue; 

and instead of swapping trigger move-assignment, more efficient. right reason valid? reason?

one of original move papers specified containers:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1858.html#23.2%20-%20sequences

and later propagated shared_ptr , function:

http://cplusplus.github.io/lwg/lwg-defects.html#743

http://cplusplus.github.io/lwg/lwg-defects.html#770

swapping rvalue arguments fell out of favor lwg 884:

http://cplusplus.github.io/lwg/lwg-defects.html#884

and n2844 subsequently removed all rvalue swaps:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2844.html

i'm not positive move. more modern shrink_to_fit() way of reducing memory, i'm not positive matters, since main use case of swapping rvalues.


Comments