Boost logo

Boost :

Subject: Re: [boost] [operators] The future
From: Matt Calabrese (rivorus_at_[hidden])
Date: 2011-10-04 17:05:45

On Tue, Oct 4, 2011 at 4:52 PM, Nathan Ridge <zeratul976_at_[hidden]> wrote:
> I meant in the case where operator+ returns a type&&.
> Then the object in question is a local variable inside operator+,
> which will not live past the point when operator+ returns.

No it's not, it's a reference to a passed in parameter. For instance:

Type&& operator +( Type&& left, Type const& right )
  left += right;
  return std::move( left );

You only return an r-value reference in the case that at least one of your
parameters is an r-value reference. The reference is to that particular
argument. Here, the first argument is returned by reference, not a local
variable. In this way, not even a move-constructor is called (nor required)
and no third object is ever created.

Anyway, as was pointed out, this is probably not a good idea for other
reasons, such as people expecting to be able to bind the result to a
reference and have its life extended (though I suppose you could just
rule-out such uses as valid when the base is used).

-Matt Calabrese

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