Boost logo

Boost :

From: Daniel Frey (d.frey_at_[hidden])
Date: 2002-07-03 09:11:00


On Wed, 2002-07-03 at 14:37, David Abrahams wrote:
> From: "Daniel Frey" <d.frey_at_[hidden]>
>
> > See also Scott Meyers "More Efficient C++", Item 4.7. If you read the
> > book, you might get the impression I misunderstood it completely, but
> > please read Scott's errata for this item, available at:
> >
> > http://www.aristeia.com/BookErrata/mec++-errata_frames.html
>
> And there it says:
>
> 7/19/96 sdm 109 In July 1996, the standardization committee
> 9/26/96
> decided that named objects may be treated
> essentially the same as unnamed objects for
> purposes of performing the return value
> optimization. I added a footnote to this effect.
>
>
> Which leads me to believe that there is no "NRVO", just an "RVO".
> Furthermore, I believe that more compilers implement the RVO for unnamed
> objects than for named objects. So why aren't you using this implementation
> instead?
>
> friend const T operator+( const T& lhs, const T& rhs )
> {
> return T(lhs) += rhs;
> }
>
> -Dave

There are several entries for Item 4.7, I refered to this one:

! 11/23/99 jm2 109 Both the first and second implementations on this
                      page suffer from the problem that they are
                      returning whatever operator=+ returns. In
                      general, there is no way to know what this is
                      (yes, it's a reference to an object of type T,
                      but a reference to which T object?), hence no
                      way for compilers to optimize away the copy to
                      operator+'s return value. The way to write
                      operator+ such that the return value
                      optimization can be performed is with this body:

                        T result(lhs);
                        result += rhs;
                        return result.

Regards, Daniel


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