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
19
votes
2 answers

Why take a std::initializer_list by rvalue reference vs. by value?

Most of the C++11 code that takes std::initializer_list I've seen takes it by value, but sometimes it is taken by rvalue reference. Is there any good reason to do this? For example, almost every example I've come across does this: class A { …
wjl
  • 7,519
  • 2
  • 32
  • 41
18
votes
2 answers

Perfect forwarding a member of object

Suppose I have two structs: struct X {}; struct Y { X x; } I have functions: void f(X&); void f(X&&); How do I write a function g() that takes Y& or Y&& but perfect forwarding X& or X&& to f(), respectively: template void g(T&& t) { …
Kan Li
  • 8,557
  • 8
  • 53
  • 93
18
votes
3 answers

Lambda closure lvalues can be passed as rvalue reference parameters

I found that lvalue lambda closures can always be passed as rvalue function parameters. See the following simple demonstration. #include #include using namespace std; void foo(std::function&& t) { } int main() { …
Anubis
  • 6,995
  • 14
  • 56
  • 87
18
votes
2 answers

What's the difference between an ordinary rvalue reference and one returned by std::forward?

I can't do this: int &&q = 7; int &&r = q; //Error Message: //cannot convert from 'int' to 'int &&' //You cannot bind an lvalue to an rvalue reference If I understand correctly, when initializing an rvalue reference, there's a temporary variable…
Rick
  • 7,007
  • 2
  • 49
  • 79
18
votes
2 answers

Why is the result of "decltype(i+j)" not an rvalue reference?

I'm trying to come up a simple example for an operation that results in a rvalue. This test case should have worked, but surprisingly (to me), the result of adding two ints is not an rvalue (reference). What am I missing here? void test(int i, int…
anderas
  • 5,744
  • 30
  • 49
18
votes
3 answers

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 rvalues. The distinguishing criterion is: if it has a…
Mustafa
  • 1,814
  • 3
  • 17
  • 25
18
votes
4 answers

Should I return an rvalue reference (by std::move'ing)?

A C++Next blog post said that A compute(…) { A v; … return v; } If A has an accessible copy or move constructor, the compiler may choose to elide the copy. Otherwise, if A has a move constructor, v is moved. Otherwise, if A has a copy…
StereoMatching
  • 4,971
  • 6
  • 38
  • 70
17
votes
1 answer

Are literal numbers mutable or not?

Naturally, this won't compile: int &z = 3; // error: invalid initialization of non-const reference .... and this will compile: const int &z = 3; // OK Now, consider: const int y = 3; int && yrr = y; // const error (as you would expect) int && yrr…
Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
17
votes
2 answers

What is an rvalue reference to function type?

I have recently wrapped my mind around the C++0x's concepts of glvalues, xvalues and prvalues, as well as the rvalue references. However, there's one thing which still eludes me: What is "an rvalue reference to function type"? It is literally…
Kos
  • 70,399
  • 25
  • 169
  • 233
17
votes
4 answers

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))(); // Error. bind(f, ref(s))(); // Error. } How can I…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
17
votes
2 answers

Why would const-ness of a local variable inhibit move semantics for the returned value?

struct STest : public boost::noncopyable { STest(STest && test) : m_n( std::move(test.m_n) ) {} explicit STest(int n) : m_n(n) {} int m_n; }; STest FuncUsingConst(int n) { STest const a(n); return a; } STest…
vschoech
  • 325
  • 1
  • 8
17
votes
2 answers

modifying the value of rvalue reference - How does it work?

The code below compiles and runs just fine. Just when I thought I'm starting to get a decent grasp on rvalue reference and std::forward - this very simple code uncovers there is something very fundamental about rvalue I don't understand. Please…
Uri London
  • 10,631
  • 5
  • 51
  • 81
16
votes
4 answers

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) = default; ~vec() = default; vec& operator+=(const…
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
16
votes
1 answer

overload resolution between lvalue reference and rvalue reference

#include using namespace std; void func(int (&ref)[6]) { cout << "#1" << endl; } void func(int * &&ref) { cout << "#2" << endl; } int main() { int arr[6]; func(arr); // g++(5.4): ambiguous, clang++(3.8): #2, vc++(19.11): #1 …
olist
  • 877
  • 5
  • 13
16
votes
2 answers

Why C++ lvalue objects can't be bound to rvalue references (&&)?

The idea of move semantics is that you can grab everything from another temporary object (referenced by an rvalue reference) and store that "everything" in your object. That helps to avoid deep copying where single construction of things is enough…
pavelkolodin
  • 2,859
  • 3
  • 31
  • 74