
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/th...). -- Aleksey Gurtovoy MetaCommunications Engineering