Boost logo

Boost :

Subject: Re: [boost] [operators] moving forward use return by reference for compilers with proper support?
From: Niall Douglas (ndouglas_at_[hidden])
Date: 2013-05-01 14:09:37

> Operators returning rvalue references are NOT safe. The C++98/03/11/14+
> apply to this scenario:
> X temporary();
> const X& r = temporary(); // ok, lives as long as r does
> X&& r2 = temporary(); // ok, lives as long as r2 does
> They do NOT apply to this scenario:
> X&& func(X&& x) { return std::move(x); }
> const X& r3 = func(temporary()); // DANGLING REFERENCE
> X&& r4 = func(temporary()); // DANGLING REFERENCE
> The Standardization Committee made this mistake when applying rvalue
> references to string's operator+(), which was fixed before C++11 was

I completely agree. 99.9% of the time the only reason to *ever* return
rvalue refs from an operator() is when you're passing through a parameter.
Otherwise, return by value and leave the compiler's optimizer do its job.


Opinions expressed here are my own and do not necessarily represent those of
BlackBerry Inc.

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