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.

1087 questions
27
votes
3 answers

Why use std::forward instead of static_cast

When given code of the following structure template void foo(Args&&... args) { ... } I've often seen library code use static_cast within the function for argument forwarding. Typically, the justification for this is that…
Curious
  • 20,870
  • 8
  • 61
  • 146
27
votes
1 answer

What's the difference between & and && in a range-based for loop?

I'm wondering what's the difference between for (auto& i : v) and for (auto&& i : v) in a range-based for loop like in this code: #include #include int main() { std::vector v = {0, 1, 2, 3, 4, 5}; std::cout <<…
LHLaurini
  • 1,737
  • 17
  • 31
26
votes
3 answers

Are there any use cases for std::forward with a prvalue?

The most common usage of std::forward is to, well, perfect forward a forwarding (universal) reference, like template void f(T&& param) { g(std::forward(param)); // perfect forward to g } Here param is an lvalue, and std::forward…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
26
votes
4 answers

Do C++11 compilers turn local variables into rvalues when they can during code optimization?

Sometimes it's wise to split complicated or long expressions into multiple steps, for example (the 2nd version isn't more clear, but it's just an example): return object1(object2(object3(x))); can be written as: object3 a(x); object2 b(a); object1…
26
votes
3 answers

C++11 rvalue reference calling copy constructor too

I've been testing some C++11 features from some some. I came across r-value references and move constructors. I implemented my first move constructor, here it is: #include #include using namespace std; class TestClass{ public: …
Luke Givens
  • 829
  • 1
  • 11
  • 15
25
votes
3 answers

Why does C++11 have implicit moves for value parameters, but not for rvalue parameters?

In C++11, value parameters (and other values) enjoy implicit move when returned: A func(A a) { return a; // uses A::A(A&&) if it exists } At least in MSVC 2010, rvalue reference parameters need std::move: A func(A && a) { return a; // uses…
Arno
  • 283
  • 3
  • 6
25
votes
1 answer

Why does the rvalue overload of `operator<<` for `basic_ostream` return an lvalue reference?

§27.7.3.9 defines the following overload for operator<<: template basic_ostream& operator<<(basic_ostream&& os, const T& x); Effects: os << x Returns: os (§27.7.2.6 defines…
Xeo
  • 129,499
  • 52
  • 291
  • 397
25
votes
3 answers

Why are rvalues references variables not rvalue?

Let's say I have two overloads of a function f void f(T&&); // #1 void f(T&); // #2 Then in the body of another function g void g(T&& t) { f(t); // calls #2 } the overload f(T&) will be called because t is considered an lvalue. This is very…
ElefEnt
  • 2,027
  • 1
  • 16
  • 20
24
votes
3 answers

What are "rvalue references for *this" for?

What are the most typical use cases of "rvalue references for *this" which the standard also calls reference qualifiers for member functions? By the way, there is a really good explanation about this language feature here.
Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120
23
votes
2 answers

Bind rvalue reference to lvalue with `void*`

While trying to understand how rvalue references work I ended up with this piece of code: int* iptr = nullptr; int*&& irr = iptr; Compiling the above code gives the following error: error: rvalue reference to type 'int *' cannot bind to lvalue of…
Nick
  • 10,309
  • 21
  • 97
  • 201
23
votes
2 answers

Object passed to std::move but not moved from?

I am reviewing some code like this, where A is a moveable type: // Returns true exactly when ownership of a is taken bool MaybeConsume(A&& a) { if (some condition) { Consume(std::move(a)); // ??? return true; } return false; } // ...…
stewbasic
  • 831
  • 7
  • 21
23
votes
4 answers

C++ "move from" container

In C++11, we can get an efficiency boost by using std::move when we want to move (destructively copy) values into a container: SomeExpensiveType x = /* ... */; vec.push_back(std::move(x)); But I can't find anything going the other way. What I mean…
GManNickG
  • 494,350
  • 52
  • 494
  • 543
23
votes
1 answer

c++11 emplace_back and push_back syntax with struct

I'm using MSVC, Visual Studio 2013. Suppose I have a struct: struct my_pair { int foo, bar; }; And I want to add a bunch of these efficiently, without too creating a temporary and then discarding it: vector v; v.push_back(41, 42); //…
stewart99
  • 14,024
  • 7
  • 27
  • 42
22
votes
2 answers

Are value parameters implicitly moved when returned by value?

Consider the following function: Foo foo(Foo x) { return x; } Will return x invoke the copy constructor or the move constructor? (Let's leave NRVO aside here.) To investigate, I wrote a simple Foo class that is only movable but not…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
22
votes
5 answers

Standard library containers producing a lot of copies on rvalues in GCC

I'm writing a app for both linux & windows, and noticed that the GCC build is producing a lot of useless calls to the copy constructor. Here's an example code to produce this behavior: struct A { A() { std::cout << "default" <<…
Inverse
  • 4,408
  • 2
  • 26
  • 35