Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of rvalue reference members?

I was wondering what use an rvalue reference member has

class A {   // ...   // Is this one useful?   Foo &&f; }; 

Does it have any benefits or drawbacks compared to an lvalue reference member? What is a prime usecase of it?

like image 548
Johannes Schaub - litb Avatar asked Jan 23 '11 14:01

Johannes Schaub - litb


People also ask

What is the use of rvalue reference?

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.

What is R value reference in C++11?

In C++11, however, the rvalue reference lets us bind a mutable reference to an rvalue, but not an lvalue. In other words, rvalue references are perfect for detecting whether a value is a temporary object or not.

What are Lvalues and Rvalues?

An lvalue (locator value) represents an object that occupies some identifiable location in memory (i.e. has an address). rvalues are defined by exclusion. Every expression is either an lvalue or an rvalue, so, an rvalue is an expression that does not represent an object occupying some identifiable location in memory.

What is the difference between lvalue and rvalue references?

“l-value” refers to a memory location that identifies an object. “r-value” refers to the data value that is stored at some address in memory. References in C++ are nothing but the alternative to the already existing variable.


2 Answers

I've seen one very motivating use case for rvalue reference data members, and it is in the C++0x draft:

template<class... Types> tuple<Types&&...> forward_as_tuple(Types&&... t) noexcept; 

Effects: Constructs a tuple of references to the arguments in t suitable for forwarding as arguments to a function. Because the result may contain references to temporary variables, a program shall ensure that the return value of this function does not outlive any of its arguments. (e.g., the program should typically not store the result in a named variable).

Returns: tuple<Types&&...>(std::forward<Types>(t)...)

The tuple has rvalue reference data members when rvalues are used as arguments to forward_as_tuple, and otherwise has lvalue reference data members.

I've found forward_as_tuple subsequently helpful when needing to catch variadic arguments, perfectly forward them packed as a tuple, and re-expand them later at the point of forwarding to a functor. I used forward_as_tuple in this style when implementing an enhanced version of tuple_cat proposed in LWG 1385:

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#1385

like image 174
Howard Hinnant Avatar answered Sep 23 '22 08:09

Howard Hinnant


According to Stephan T. Lavavej, rvalue reference data members have no use.

[at 31:00] The thing I've seen programmers do when they get hold of rvalue references is that, they start to go a little crazy, because they're so powerful. They start saying "Oh, I'm gonna have rvalue reference data members, I'm gonna have rvalue reference local variables, I'm gonna have rvalue reference return values!" And then they write code like this: [...]

like image 25
fredoverflow Avatar answered Sep 24 '22 08:09

fredoverflow