
on Tue Sep 08 2009, Ion Gaztañaga <igaztanaga-AT-gmail.com> wrote:
David Abrahams escribió:
Well, maybe benefits don't outweigh problems... I added that overload so that assignment moves rvalues instead of copying them,
In the first version of the library we had two overloads ("const T &" and "rv<T> &") for copy/move constructor and assignments. But if T& operator=(const T&) is defined then:
T produce();
T t; //... //Copy assignment called instead of move-assignment! t = produce();
This is suboptimal for assignments, because a move is replaced with a copy. In theory this also happens for copy-constructors but fortunately RVO is a much better approach and in practice this problem only happens with assignments:
...and only with assignments that don't already take their argument by value. std::vector assignment would be an example of something that can't.
So right know I have two options: the old approach that produces suboptimal assignments from non-const rvalues and the newer one that is optimal but hurts the compiler generated copy assignment.
We need a third alternative: a new overload set that maintains "const T &" and properly catches non-const rvalues.
Hmm, tough one. -- Dave Abrahams BoostPro Computing http://www.boostpro.com