Is there a reason when a function should return a RValue Reference? A technique, or trick, or an idiom or pattern?
MyClass&& func( ... );
I am aware of the danger of returning references in general, but sometimes we do it anyway, don't we? T& T::operator=(T)
is just one idiomatic example. But how about T&& func(...)
? Is there any general place where we would gain from doing that? Probably different when one writes library or API code, compared to just client code?
Rvalue references is a small technical extension to the C++ language. Rvalue references allow programmers to avoid logically unnecessary copying and to provide perfect forwarding functions. They are primarily meant to aid in the design of higer performance and more robust libraries.
11. Don't return references. Don't use std::move() on rvalues. Functions return an rvalue without you doing anything, they already do this (see point #1).
Rvalue references are used to indicate whether the recipient will move /pilfer/otherwise render inert (but valid) the referent, not copy them. Copy semantics should be indicated by const & or value.
The lifetime of a temporary object may be extended by binding to a const lvalue reference or to an rvalue reference (since C++11), see reference initialization for details.
There are a few occasions when it is appropriate, but they are relatively rare. The case comes up in one example when you want to allow the client to move from a data member. For example:
template <class Iter>
class move_iterator
{
private:
Iter i_;
public:
...
value_type&& operator*() const {return std::move(*i_);}
...
};
This follows up on towi's comment. You never want to return references to local variables. But you might have this:
vector<N> operator+(const vector<N>& x1, const vector<N>& x2) { vector<N> x3 = x1; x3 += x2; return x3; }
vector<N>&& operator+(const vector<N>& x1, vector<N>&& x2) { x2 += x1; return std::move(x2); }
vector<N>&& operator+(vector<N>&& x1, const vector<N>& x2) { x1 += x2; return std::move(x1); }
vector<N>&& operator+(vector<N>&& x1, vector<N>&& x2) { x1 += x2; return std::move(x1); }
This should prevent any copies (and possible allocations) in all cases except where both parameters are lvalues.
No. Just return the value. Returning references in general is not at all dangerous- it's returning references to local variables which is dangerous. Returning an rvalue reference, however, is pretty worthless in almost all situations (I guess if you were writing std::move
or something).
You can return by reference if you are sure the referenced object will not go out of scope after the function exits, e.g. it's a global object's reference, or member function returning reference to class fields, etc.
This returning reference rule is just same to both lvalue and rvalue reference. The difference is how you want to use the returned reference. As I can see, returning by rvalue reference is rare. If you have function:
Type&& func();
You won't like such code:
Type&& ref_a = func();
because it effectively defines ref_a as Type& since named rvalue reference is an lvalue, and no actual move will be performed here. It's quite like:
const Type& ref_a = func();
except that the actual ref_a is a non-const lvalue reference.
And it's also not very useful even you directly pass func() to another function which takes a Type&& argument because it's still a named reference inside that function.
void anotherFunc(Type&& t) {
// t is a named reference
}
anotherFunc(func());
The relationship of func( ) and anotherFunc( ) is more like an "authorization" that func() agrees anotherFunc( ) might take ownership of (or you can say "steal") the returned object from func( ). But this agreement is very loose. A non-const lvalue reference can still be "stolen" by callers. Actually functions are rarely defined to take rvalue reference arguments. The most common case is that "anotherFunc" is a class name and anotherFunc( ) is actually a move constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With