Boost logo

Boost :

From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2005-03-04 03:31:51


Pavel Chikulaev writes:
> Do you know that well-known Herb Sutter's assignment operator in most cases
> does unnecessary copying? No? Then read it to learn how to elminate it.

[...]
>
> But how can we define that copying is necessary? - Every function returns
> object not of type T but temporary object of type T. So let's code
> something:
>
> template<typename T>
> struct temporary : T
> {
> temporary() {}
> temporary(const T & t) : T(t) {}
>
> //some operators and constructors to create base object in-place not
> listed here
> };
>
> Now let's change function consider operator +:
>
> Matrix operator + (const Matrix & a, const Matrix & b)
> {
> Matrix temp(a);
> temp += b;
> return temp;
> }
>
> To optimize it we do the following:
>
> temporary<Matrix> operator + (const Matrix & a, const Matrix & b)
> {
> Matrix temp(a);
> temp += b;
> return temp;
> }

[...]

That's called "move semantics", a library-based approach to which was
thought of as far back as 2001 -- http://tinyurl.com/4nzcq
(http://groups-beta.google.com/group/comp.lang.c++.moderated/browse_thread/thread/a742757808cb881f).

-- 
Aleksey Gurtovoy
MetaCommunications Engineering

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk