Boost logo

Boost :

From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2002-10-12 07:26:04

>From: "Daniel Frey" d.frey_at_[hidden]

> On Sat, 12 Oct 2002 05:40:19 +0200, David Abrahams wrote:
> > Daniel Frey <daniel.frey_at_[hidden]> writes:
> >
> >> > const T operator+(const T& lhs, const T& rhs) {
> >> > T copy(lhs);
> >> > return copy += rhs;
> >> > }
> >>
> >> This is completly wrong, as I already said multiple times. See
> >> operators_1_28_0.patch.gz in the boost files section and search the
> >> mailing list for the corresponding discussion.
> >
> > Daniel,
> >
> > Did we ever apply your patch? Now might be a good time, as Boost 1.29.0
> > is now released. Were there outstanding issues?
> The patch wasn't applied, but from my side there are no outstanding
> issues. I think it wasn't applied because everyone was busy with more
> important things, like the new libraries for 1.29.0.
> Obviously we have still to figure out which compilers do support the
> NRVO, but I can't do this for compilers I don't have :) I hope the
> documentation is acceptable, if it still needs refinement, please let me
> know.

I think it would be good to get this patch applied. I looked at
operators.hpp a while ago, and found the asymmetric parameters rather
inelegant. I.e.:

friend const T operator+( T lhs, const T& rhs )
   return lhs += rhs;

The patch uses:

friend T operator+( const T& lhs, const T& rhs )
   T nrv( lhs );
   nrv += rhs;
   return nrv;

A couple of things I wonder about:

- The posting with the patch
( says "See also
Scott Meyers "More Efficient C++", Item 4.7.". There's no Item 4.7, and Item
4 doesn't seem to apply, so I wonder what is referred to, here?

- The same posting uses "friend const T operator+()" in the posting, but
"friend T operator+()" in the patch. Which one will be used, and why?

"Effective C++", Item 21, "Use const whenever possible" argues for returning
const UDTs, to avoid operating on temporaries, such as "(a * b)=c". This
would also be illegal for built-in types. Why then not return "const T" in
operators.hpp? The const is ignored for built-in types (as they have no
"this"), but prevents the operation on temporaries, such as the above.

- "More Effective C++", Item 20, "Facilitate the return value optimization"
argues for using the constructor in the return statement, rather than
creating a named temp. The argument is that although NRVO is also now
possible, RVO (unnamed temporary) has been around longer, and may therefore
be more widely implemented. Thus, compilers implementing NRVO almost
certainly implements RVO, but not necessarily the other way around. Given
this, why isn't the following used in operators.hpp:

friend T operator+( const T& lhs, const T& rhs )
   return T(lhs)+=rhs;



Boost list run by bdawes at, gregod at, cpdaniel at, john at