Boost logo

Boost :

From: Daniel Frey (d.frey_at_[hidden])
Date: 2002-10-22 17:06:38


On Tue, 22 Oct 2002 22:14:26 +0200, David Abrahams wrote:

> Daniel Frey <d.frey_at_[hidden]> writes:
>
>> this is equivalent to:
>>
>> T operator+( const T& lhs, const T& rhs ) {
>> return T( lhs ).operator+=( rhs );
>> }
>
> Not neccessarily. It's possible to implement += as a free function. I'm
> not sure if that's relevant to your argument or just a red herring.

Well, OK, it could be a free function, but

   return operator+=( T( lhs ), rhs );

doesn't look any better wrt RVO, does it? ;)

>> If the compiler implements NRVO, it cannot be made any better - the
>> temporary is simply eliminated completely. If the compiler doesn't
>> implement the NRVO, Mojo may be the best pattern. As you can see from
>> the patch to operators.hpp, boost can provide both. Maybe you want to
>> add a Mojo-friendly implementation of the non-NRVO version? ;)
>
> I don't know how well that works. You need to know that T implements the
> appropriate constructor, which AFAICT isn't possible for a generic
> function.

Are you talking about a Mojo-friendly implementation? Andrei can probably
provide a better answer then and let's hope he answers in a language I understand
(as he seems to speak a *lot* of languages :-))

> I offered to run any test program posted, but nobody gave me one to try.

Oh, I think I can change that:

#include <iostream>
using namespace std;

struct X
{
   X() { cout << "X"; }
   X( const X& ) { cout << "C"; }
};

X f()
{
   return X();
}

X g()
{
   X x;
   return x;
}

int main()
{
   X x1( f() ); cout << endl;
   X x2( g() ); cout << endl;
}

Regards, Daniel


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