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.

rvalue-reference
Filter by
Sorted by
Tagged with
349 votes
9 answers
80k views

Rule-of-Three becomes Rule-of-Five with C++11? [closed]

So, after watching this wonderful lecture on rvalue references, I thought that every class would benefit of such a "move constructor", template<class T> MyClass(T&& other) edit and of ...
Xeo's user avatar
  • 131k
581 votes
8 answers
123k views

What are the main purposes of std::forward and which problems does it solve?

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 ...
Steveng's user avatar
  • 5,931
523 votes
6 answers
141k views

C++11 rvalues and move semantics with 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<...
Tarantula's user avatar
  • 19.4k
48 votes
4 answers
13k views

Is an rvalue reference treated as an lvalue when used within a function?

I posted this answer: https://stackoverflow.com/a/28459180/2642059 Which contains the following code: void foo(string&& bar){ string* temp = &bar; cout << *temp << " @...
Jonathan Mee's user avatar
  • 38.4k
1087 votes
4 answers
347k views

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 ...
paxdiablo's user avatar
  • 869k
160 votes
2 answers
103k views

Is returning by rvalue reference more efficient?

for example: Beta_ab&& Beta::toAB() const { return move(Beta_ab(1, 1)); }
Neil G's user avatar
  • 32.6k
88 votes
1 answer
29k views

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 ...
vmpstr's user avatar
  • 5,071
214 votes
7 answers
131k views

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<int> myPointer(new int); std::function<void(void)> ...
user avatar
130 votes
8 answers
26k views

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's user avatar
26 votes
3 answers
4k views

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 ...
ElefEnt's user avatar
  • 2,107
73 votes
1 answer
6k views

Overload resolution between value, rvalue reference, const lvalue reference

Given all three functions, this call is ambiguous. int f( int ); int f( int && ); int f( int const & ); int q = f( 3 ); Removing f( int ) causes both Clang and GCC to prefer the rvalue ...
Potatoswatter's user avatar
141 votes
2 answers
15k views

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

The std::move(x) function doesn't really move anything at all. It is just a cast to an rvalue. Why was this done? Isn't this misleading?
Howard Hinnant's user avatar
30 votes
3 answers
4k views

What is the rationale behind forwarding references and rvalue references sharing the same syntax? [duplicate]

This is an rvalue reference: void foo(int&& a); It does not bind to lvalues: int i = 42; foo(i); // error This is a universal reference: template<typename T> void bar(T&& ...
fredoverflow's user avatar
82 votes
5 answers
17k views

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 ...
towi's user avatar
  • 21.9k
86 votes
7 answers
22k views

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 ...
jbgs's user avatar
  • 2,825
52 votes
2 answers
25k views

Why user-defined move-constructor disables the implicit copy-constructor?

While I'm reading boost/shared_ptr.hpp, i saw this code: // generated copy constructor, destructor are fine... #if defined( BOOST_HAS_RVALUE_REFS ) // ... except in C++0x, move disables the ...
amazingjxq's user avatar
  • 4,577
32 votes
4 answers
4k views

What's a use case for overloading member functions on reference qualifiers?

C++11 makes it possible to overload member functions based on reference qualifiers: class Foo { public: void f() &; // for when *this is an lvalue void f() &&; // for when *this is ...
KnowItAllWannabe's user avatar
43 votes
6 answers
8k views

How to actually implement the rule of five?

UPDATE at the bottom q1: How would you implement the rule of five for a class that manages rather heavy resources, but of which you want it to be passed around by value because that greatly ...
stijn's user avatar
  • 35.3k
34 votes
2 answers
1k views

non-class rvalues always have cv-unqualified types

§3.10 section 9 says "non-class rvalues always have cv-unqualified types". That made me wonder... int foo() { return 5; } const int bar() { return 5; } void pass_int(int&& i) { ...
fredoverflow's user avatar
18 votes
3 answers
4k views

On how to recognize Rvalue or Lvalue reference and if-it-has-a-name rule

I was reading Thomas Becker's article on rvalue reference and their use. In there he defines what he calls if-it-has-a-name rule: Things that are declared as rvalue reference can be lvalues or ...
Mustafa's user avatar
  • 1,824
9 votes
2 answers
2k views

In C++, what categories (lvalue, rvalue, xvalue, etc.) can expressions that produce temporaries of class type fall into?

Here is some example code: #include <iostream> class Foo { public: explicit Foo(int x) : data(x) {}; Foo& operator++() { data += 1; return *this; } void *get_addr() { ...
CppNoob's user avatar
  • 2,362
68 votes
2 answers
9k views

Can I typically/always use std::forward instead of std::move?

I've been watching Scott Meyers' talk on Universal References from the C++ and Beyond 2012 conference, and everything makes sense so far. However, an audience member asks a question at around 50 ...
Joseph Mansfield's user avatar
34 votes
5 answers
14k views

Passing rvalues through std::bind

I want to pass an rvalue through std::bind to a function that takes an rvalue reference in C++0x. I can't figure out how to do it. For example: #include <utility> #include <functional> ...
Timothy003's user avatar
  • 2,358
23 votes
3 answers
12k views

When is a reference a forwarding reference, and when is it an rvalue reference?

I understand that a forwarding reference is "an rvalue reference to a cv-unqualified template parameter", such as in template <class T> void foo(T&& ); which means the above ...
Dave 's user avatar
  • 269
14 votes
2 answers
5k views

Is it possible to std::move objects out of functions? (C++11)

This program tries to move a string out of a function and use it for the construction of another string: #include <iostream> #include <string> #include <utility> std::string &&...
Trevor Hickey's user avatar
77 votes
4 answers
35k views

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; ...
Display Name's user avatar
  • 2,343
26 votes
3 answers
6k views

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 <iostream> #include &...
Luke Givens's user avatar
7 votes
3 answers
1k views

Why is a named rvalue reference an lvalue expression?

I know that a named reference is an lvalue: int x = 1; int& ref1 = x; int&& ref2 = std::move(x); I've read the explanation — that is because we can take the address of those ref1 ...
beginpluses's user avatar
51 votes
5 answers
12k views

How can I make a forwarding-reference parameter bind only to rvalue references?

I'm writing a network library and use move semantics heavily to handle ownership for file descriptors. One of my classes wishes to receive file descriptor wrappers of other kinds and take ownership, ...
Ralph Zhang's user avatar
  • 5,141
50 votes
4 answers
13k views

Should a move constructor take a const or non-const rvalue reference?

In several places I've seen the recommended signatures of copy and move constructors given as: struct T { T(); T(const T& other); T(T&& other); }; Where the copy constructor ...
Ben Hymers's user avatar
  • 26.1k
20 votes
3 answers
2k views

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 … ...
Suma's user avatar
  • 33.9k
17 votes
1 answer
2k views

Extending temporary's lifetime through rvalue data-member works with aggregate, but not with constructor, why?

I've found the following scheme to extend a temporaries lifetime works, I don't know if it should, but it does. struct S { std::vector<int>&& vec; }; int main() { S s1{std::...
Ryan Haining's user avatar
  • 36.2k
15 votes
3 answers
2k views

How does std::forward receive the correct argument?

Consider: void g(int&); void g(int&&); template<class T> void f(T&& x) { g(std::forward<T>(x)); } int main() { f(10); } Since the id-expression x is an ...
template boy's user avatar
  • 10.4k
43 votes
4 answers
12k views

What is use of the ref-qualifier `const &&`?

I've been digging around ref-qualifiers a bit, following on a previous question. Given the code sample below; #include <iostream> #include <string> #include <utility> struct A { ...
Niall's user avatar
  • 30.4k
22 votes
5 answers
5k views

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 &...
moswald's user avatar
  • 11.6k
17 votes
4 answers
5k views

Is there a reference_wrapper<> for rvalue references?

I wonder how the following can be done void f(string &&s) { std::string i(move(s)); /* other stuff */ } int main() { std::string s; bind(f, s)(); // Error. bind(f, move(s))();...
Johannes Schaub - litb's user avatar
13 votes
1 answer
1k views

Why doesn't C++ move construct rvalue references by default? [duplicate]

Say I have the following function void doWork(Widget && param) // param is an LVALUE of RRef type { Widget store = std::move(param); } Why do I need to cast param back to an rvalue ...
barney's user avatar
  • 2,192
7 votes
1 answer
674 views

Operator overload which permits capturing with rvalue but not assigning to

Is it possible to design and how should I make overloaded operator+ for my class C to have this possible: C&& c = c1 + c2; but this not possible: c1 + c2 = something; Edit: I changed ...
Argbart's user avatar
  • 73
152 votes
1 answer
68k views

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:...
Edward Strange's user avatar
15 votes
2 answers
3k views

Reason to use std::move on rvalue reference parameter

I was reading a book about data structure implemented in C++, I dont understand a code snippet, it's part of vector class void push_back(object &&x) { //do something objects[...
codesavesworld's user avatar
-2 votes
2 answers
1k views

problems with Move constructor and Move overloaded assignment operator?

Mostly all things explained by fredoverflow(user 237K Rep.) in his Two answers https://stackoverflow.com/a/3109981/11862989 https://stackoverflow.com/a/11540204/11862989 But while implementing Move ...
Abhishek Mane's user avatar
76 votes
6 answers
60k views

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; ...
Grapes's user avatar
  • 2,513
27 votes
3 answers
6k views

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 ...
Arno's user avatar
  • 303
16 votes
4 answers
7k views

Overloading on R-value references and code duplication

Consider the following: struct vec { int v[3]; vec() : v() {}; vec(int x, int y, int z) : v{x,y,z} {}; vec(const vec& that) = default; vec& operator=(const vec& that) ...
R. Martinho Fernandes's user avatar
3 votes
1 answer
2k views

Why can an rvalue not bind to a non-const lvalue reference, other than the fact that writing to a temporary has no effect?

I have read the SO question here and understood this part of the answer: "But if you bind a temporary to a non-const reference, you can keep passing it around "forever" just to have your manipulation ...
Mario Ishac's user avatar
  • 5,467
1 vote
1 answer
2k views

Ambiguous call with overloaded r-value reference function [duplicate]

I have a class with the following declarations: class IcoSphere { [...] private: int _addVertex(const glm::vec3 &p); int addVertex(glm::vec3 p); int addVertex(const glm::vec3 &&...
Silverlan's user avatar
  • 2,832
44 votes
4 answers
2k views

Avoid exponential grow of const references and rvalue references in constructor

I am coding some templated classes for a machine learning library, and I'm facing this issue a lot of times. I'm using mostly the policy pattern, where classes receive as template argument policies ...
Federico Allocati's user avatar
34 votes
1 answer
39k views

Lvalue to rvalue reference binding

The compiler keeps complaining I'm trying to bind an lvalue to an rvalue reference, but I cannot see how. I'm new to C++11, move semantics, etc., so please bear with me. I have this function: ...
Kristian D'Amato's user avatar
26 votes
4 answers
3k views

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 ...
Michał Fronczyk's user avatar
24 votes
3 answers
2k views

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 ...
Ralph Tandetzky's user avatar

1
2 3 4 5