Boost logo

Boost :

From: Daniel Frey (d.frey_at_[hidden])
Date: 2002-07-03 06:17:41


On Wed, 2002-07-03 at 12:46, Peter Dimov wrote:
> From: "Yitzhak Sapir" <yitzhaks_at_[hidden]>
> > I had to view Daniel Frey's mail as an attachment, but from reading it I
> have a question:
> >
> > He suggested:
> > > friend const T operator+( const T& lhs, const T& rhs )
> > > {
> > > T nrv( lhs );
> > > nrv += rhs;
> > > return nrv;
> > > }
> >
> > Why not use:
> >
> > friend const T operator+ (const T& lhs, const T& rhs)
> > {
> > return T(lhs) += rhs;
> > }
> >
> > doing away with the named temporary, and possibly using normal return
> value optimization?
>
> Because this assumes that the compiler knows that T::operator+= returns
> *this.

Which won't help if he knows it as the standard doesn't allow it to
optimize this case. This is because of the observable side-effects. The
standard only allows certain exceptions to this rule, namely the
RVO/NRVO. Your (Yitzhak's) implementation doesn't match the NRVO/RVO
rules, thus it is not allowed to be optimized.

Regards, Daniel


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