From: Pavel Vozenilek (pavel_vozenilek_at_[hidden])
Date: 2002-10-15 16:09:55
>From: "Terje Slettebø" <tslettebo_at_[hidden]>
>>From: "Pavel Vozenilek" <pavel_vozenilek_at_[hidden]>
>> operator+() can be implemented like described in
>> http://www.semantics.org/gotchas/gotcha36.pdf (Stewe Dewhurst's
> Yeah, but that would be cheating. :) ....
> ... In other words, getting the wrong result fast is never a bargain. :)
Cheating is way too strong word. It is technique that may or may not speedup
the code, e.g. :
- only one constructor is called (thus dynamically allocated members (if
need to be allocated only once and are not reallocated)
- depending on the class, some code otherwise invoked during copy can be
- it is ideal situation for RVO
> Test(const Test& lhs, const Test& rhs) : num(lhs.num + rhs.num)
> And then we're back to having a copy in the code, after all.
If this trick is not used, memcpy() will likely be called twice: during copy
and then during operator+=().
I once modified Alexandresu'c flex_string<> and time needed for operator+()
dropped down to 50-75%, depending on string length (with VC++ 6.0).
IMHO this technique makes sense for very performance sensitive classes only
standard copy constructor and operator+=() need to be coded separately,
resulting in code duplication.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk