Questions tagged [rvalue-reference]

An rvalue reference is a new language feature in C++11 representing a reference to an rvalue. Together with reference collapsing, they are used to implement and enable move semantics and perfect forwarding.

An rvalue reference is a language feature added in C++11 (formerly known as C++0x). It is used to bind to an rvalue thus extending the temporary object's lifetime.

Together with reference collapsing, they are used to implement and enable move semantics and perfect forwarding.

979 questions
887
votes
4 answers

What does T&& (double ampersand) mean in C++11?

I've been looking into some of the new features of C++11 and one I've noticed is the double ampersand in declaring variables, like T&& var. For a start, what is this beast called? I wish Google would allow us to search for punctuation like…
paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
493
votes
6 answers

What are the main purposes of using std::forward and which problems it solves?

In perfect forwarding, std::forward is used to convert the named rvalue references t1 and t2 to unnamed rvalue references. What is the purpose of doing that? How would that affect the called function inner if we leave t1 & t2 as lvalues? template…
Steveng
  • 5,051
  • 4
  • 14
  • 7
474
votes
6 answers

C++11 rvalues and move semantics confusion (return statement)

I'm trying to understand rvalue references and move semantics of C++11. What is the difference between these examples, and which of them is going to do no vector copy? First example std::vector return_vector(void) { std::vector tmp…
Tarantula
  • 16,620
  • 11
  • 48
  • 70
336
votes
8 answers

Rule-of-Three becomes Rule-of-Five with C++11?

So, after watching this wonderful lecture on rvalue references, I thought that every class would benefit of such a "move constructor", template MyClass(T&& other) edit and of course a "move assignment operator", template MyClass&…
Xeo
  • 123,374
  • 44
  • 277
  • 381
173
votes
6 answers

Move capture in lambda

How do I capture by move (also known as rvalue reference) in a C++11 lambda? I am trying to write something like this: std::unique_ptr myPointer(new int); std::function example = [std::move(myPointer)]{ *myPointer = 4; };
Lalaland
  • 8,180
  • 3
  • 30
  • 47
141
votes
2 answers

Is returning by rvalue reference more efficient?

for example: Beta_ab&& Beta::toAB() const { return move(Beta_ab(1, 1)); }
Neil G
  • 28,787
  • 31
  • 143
  • 234
133
votes
2 answers

Why is `std::move` named `std::move`?

The C++11 std::move(x) function doesn't really move anything at all. It is just a cast to r-value. Why was this done? Isn't this misleading?
Howard Hinnant
  • 179,402
  • 46
  • 391
  • 527
122
votes
1 answer

How would one call std::forward on all arguments in a variadic function?

I was just writing a generic object factory and using the boost preprocessor meta-library to make a variadic template (using 2010 and it doesn't support them). My function uses rval references and std::forward to do perfect forwarding and it got me…
95
votes
5 answers

Do rvalue references to const have any use?

I guess not, but I would like to confirm. Is there any use for const Foo&&, where Foo is a class type?
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
85
votes
1 answer

C++11 make_pair with specified template parameters doesn't compile

I was just playing around with g++ 4.7 (one of the later snapshots) with -std=c++11 enabled. I tried to compile some of my existing code base and one case that failed somewhat confuses me. I would appreciate if someone can explain what is going…
vmpstr
  • 4,841
  • 2
  • 23
  • 24
80
votes
5 answers

Is there any case where a return of a RValue Reference (&&) is useful?

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&…
towi
  • 20,210
  • 25
  • 94
  • 167
74
votes
7 answers

Is the pass-by-value-and-then-move construct a bad idiom?

Since we have move semantics in C++, nowadays it is usual to do void set_a(A a) { _a = std::move(a); } The reasoning is that if a is an rvalue, the copy will be elided and there will be just one move. But what happens if a is an lvalue? It seems…
jbgs
  • 2,515
  • 2
  • 17
  • 26
67
votes
1 answer

Workarounds for no 'rvalue references to *this' feature

I have a proxy container class around a movable object, and wish the proxy to be able to implicitly yield an rvalue reference to the underlying object, but only when the proxy itself is being moved. I believe that I will be able to implement this…
boycy
  • 1,433
  • 12
  • 24
65
votes
4 answers

Why do some people use swap for move assignments?

For example, stdlibc++ has the following: unique_lock& operator=(unique_lock&& __u) { if(_M_owns) unlock(); unique_lock(std::move(__u)).swap(*this); __u._M_device = 0; __u._M_owns = false; return *this; } Why not just…
Display Name
  • 1,940
  • 1
  • 21
  • 40
63
votes
5 answers

Correct usage of rvalue references as parameters

Let's take the following method as an example: void Asset::Load( const std::string& path ) { // complicated method.... } General use of this method would be as follows: Asset exampleAsset; exampleAsset.Load("image0.png"); Since we know most of…
Grapes
  • 2,225
  • 3
  • 20
  • 40
1
2 3
65 66