Re: [boost] Improving the assignment operators of various Boosttypes

Giovanni Piero Deretta wrote:
If you do not want to deal with move emulation (which I've found very brittle in complex expressions), a simple way to to gain the advantage of T::operator=(T rhs) even when assigning from lvalues is something like:
templateT destructive_copy(T& x) { using std::swap; T result; swap(result, x); return x; }
which relies on NRVO to eliminate expensive copies:
Shouldn't the last statement be return result; instead? Or maybe I am missing something? Best Regards, Gevorg

On Thu, Sep 11, 2008 at 9:34 AM, Gevorg Voskanyan <v_gevorg@yahoo.com> wrote:
Giovanni Piero Deretta wrote:
If you do not want to deal with move emulation (which I've found very brittle in complex expressions), a simple way to to gain the advantage of T::operator=(T rhs) even when assigning from lvalues is something like:
templateT destructive_copy(T& x) { using std::swap; T result; swap(result, x); return x; }
which relies on NRVO to eliminate expensive copies:
Shouldn't the last statement be return result; instead? Or maybe I am missing something?
Yes, of course :), sorry for the typo... -- gpd

Gevorg Voskanyan skrev:
Giovanni Piero Deretta wrote:
If you do not want to deal with move emulation (which I've found very brittle in complex expressions), a simple way to to gain the advantage of T::operator=(T rhs) even when assigning from lvalues is something like:
templateT destructive_copy(T& x) { using std::swap; T result; swap(result, x); return x; }
which relies on NRVO to eliminate expensive copies:
I think the best way (= most RVOs) to write the assignment operator is to take the T argument by value. -Thorsten

On Thu, Sep 11, 2008 at 11:06 AM, Thorsten Ottosen <thorsten.ottosen@dezide.com> wrote:
Gevorg Voskanyan skrev:
Giovanni Piero Deretta wrote:
If you do not want to deal with move emulation (which I've found very brittle in complex expressions), a simple way to to gain the advantage of T::operator=(T rhs) even when assigning from lvalues is something like:
templateT destructive_copy(T& x) { using std::swap; T result; swap(result, x); return x; }
which relies on NRVO to eliminate expensive copies:
I think the best way (= most RVOs) to write the assignment operator is to take the T argument by value.
Uh? I do not understand how your statement relates to the code above. The point of destructive_copy is to move out of lvalues (by 'converting' them to rvalues), not optimizing out copies of rvalues. -- gpd

Giovanni Piero Deretta skrev:
On Thu, Sep 11, 2008 at 11:06 AM, Thorsten Ottosen <thorsten.ottosen@dezide.com> wrote:
I think the best way (= most RVOs) to write the assignment operator is to take the T argument by value.
Uh? I do not understand how your statement relates to the code above. The point of destructive_copy is to move out of lvalues (by 'converting' them to rvalues), not optimizing out copies of rvalues.
Oh. I thought this was a new thread about the assignment operator. It appeared as a new thread in my news reader. -Thorsten
participants (3)
-
Gevorg Voskanyan
-
Giovanni Piero Deretta
-
Thorsten Ottosen