Boost logo

Boost :

Subject: Re: [boost] [operators] The future
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2011-10-04 16:52:56


> >>> If the operations return "type&&" instead of "type", they can potentially be
> >>> more efficient, but I'm a little cautious to do that without giving it some
> >>> thought because the choice implies subtle differences. I'm sure Daniel has
> >>> already taken this all into consideration.
> >>
> >> Return by value please. Otherwise:
> >>
> >> type const& x = type() + type();
> >> std::cout << x; // BOOM; x is dangling
> >
> > Won't the following fail too, making it completely useless?
> >
> > void foo(type const& x)
> > {
> > ...
> > }
> >
> > foo(type() + type());
>
> No, because the temporary created by "type() + type()" is guaranteed to
> live until after foo returns.

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.

Of course the case when operator+ returns a type is fine.

Regards,
Nate
                                               


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