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
22
votes
5 answers

Understanding rvalue references

I think there's something I'm not quite understanding about rvalue references. Why does the following fail to compile (VS2012) with the error 'foo' : cannot convert parameter 1 from 'int' to 'int &&'? void foo(int &&) {} void bar(int &&x) { foo(x);…
moswald
  • 11,491
  • 7
  • 52
  • 78
21
votes
2 answers

Preventing non-const lvalues from resolving to rvalue reference instead of const lvalue reference

I'm having trouble overloading a function to take a value either by const reference or, if it is an rvalue, an rvalue reference. The problem is that my non-const lvalues are binding to the rvalue version of the function. I'm doing this in…
Ayjay
  • 3,413
  • 15
  • 20
21
votes
3 answers

Can std::string overload "substr" for rvalue *this and steal resources?

It just occurred to me I noticed that std::string's substr operation could be much more efficient for rvalues when it could steal the allocated memory from *this. The Standard library of N3225 contains the following member function declaration of…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
21
votes
3 answers

Rvalue to lvalue conversion?

Why it is not possible to convert rvalues to lvalues? It is possible to do a conversion in the opposite direction though. Technically rvalues do have a memory address, isn't it?
user8194556
20
votes
1 answer

Why use identity in forward definition for C++0x rvalue reference?

In A Brief Introduction to Rvalue References, forward is defined as follows: template struct identity { typedef T type; }; template T &&forward(typename identity::type &&a) { return a; } What purpose does the…
user2023370
  • 10,488
  • 6
  • 50
  • 83
20
votes
3 answers

C++ forwarding reference and r-value reference

I understand that a forwarding reference is "an rvalue reference to a cv-unqualified template parameter", such as in template void foo(T&& ); which means the above function can take both l-value and r-value reference. There's something…
Dave
  • 239
  • 1
  • 2
  • 6
20
votes
7 answers

Use of rvalue references in function parameter of overloaded function creates too many combinations

Imagine you have a number of overloaded methods that (before C++11) looked like this: class MyClass { public: void f(const MyBigType& a, int id); void f(const MyBigType& a, string name); void f(const MyBigType& a, int b, int c, int d); …
alexc
  • 534
  • 2
  • 9
20
votes
2 answers

Can std::function be move-constructed from rvalue reference to a temporary functor object?

I have an untemplated functor object that I'm trying to store as a std::function inside another object. This object is really heavyweight, so it's marked as uncopyable, but it does have a move constructor. However, trying to construct a…
Crashworks
  • 40,496
  • 12
  • 101
  • 170
20
votes
3 answers

Can compiler generate std::move for a last use of lvalue automatically?

A code like this is often seen in r-value references articles: Dave Abrams: Move It With Rvalue References void g(X); void f() { X b; g(b); // still need the value of b … g( std::move(b) ); // all done with b now; grant…
Suma
  • 33,181
  • 16
  • 123
  • 191
20
votes
2 answers

Lifetime extension and the conditional operator

local lvalue references-to-const and rvalue references can extend the lifetime of temporaries: const std::string& a = std::string("hello"); std::string&& b = std::string("world"); Does that also work when the initializer is not a simple expression,…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
19
votes
2 answers

What does && mean with a parameter type in C++?

Possible Duplicate: What does T&& mean in C++0x? I had never seen a double ampersand before I read this answer. The code snippet in question is this: template T& as_lvalue(T&& x) { return x; } What does && achieve? What sorts…
paperjam
  • 8,321
  • 12
  • 53
  • 79
19
votes
4 answers

Is it possible to obtain the address of the 'this' pointer?

I read that this is an rvalue and we cannot get its address by applying &this. In my code, I have tried to use a reference binding to this. I'm wondering which way will give the address of this? Or are both wrong? What exactly is this? An lvalue, an…
Range Hao
  • 211
  • 2
  • 5
19
votes
1 answer

Why does this rvalue reference bind to an lvalue?

I do not understand why the following code compiles on GCC 8.0: decltype(auto) foo(int&& r) { return r; } In foo, the declaration type of r is int&&, and so the return type of foo is also int&&. But r itself is an lvalue, and an lvalue cannot…
curiousguy12
  • 1,741
  • 1
  • 10
  • 15
19
votes
2 answers

C++11 lambda can be assigned to std::function with incorrect signature

The following compiles and runs (under Apple LLVM version 6.1.0 and Visual C++ 2015): #include #include struct s { int x; }; int main(int argc, char **argv) { std::function f = [](const s &p) { std::cout <<…
Eagle
  • 201
  • 1
  • 4
19
votes
4 answers

How to reduce redundant code when adding new c++0x rvalue reference operator overloads

I am adding new operator overloads to take advantage of c++0x rvalue references, and I feel like I'm producing a lot of redundant code. I have a class, tree, that holds a tree of algebraic operations on double values. Here is an example use…
Inverse
  • 4,408
  • 2
  • 26
  • 35