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
14
votes
8 answers

What kinds of code need to be aware of rvalue references?

I see lots of people having problems understanding rvalue references. Will "ordinary" C++ code (e.g., that uses the standard library, but doesn't implement it) need to know about rvalue references once the new standard comes out, or can the…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
14
votes
7 answers

Passing non-const references to rvalues in C++

In the following line of code: bootrec_reset(File(path, size, off), blksize); Calling a function with prototype: static void bootrec_reset(File &file, ssize_t blksize); I receive this error: libcpfs/mkfs.cc:99:53: error: invalid initialization of…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
14
votes
1 answer

Type of member variable should depend on constructor argument's type

I try to define a class A as follows: template< typename T > class A { public: A( T elem ) : _elem( elem ) {} private: TYPE _elem; // "TYPE" should be either "T" in case "elem" is an r-value or "T&" in case "elem" is an…
abraham_hilbert
  • 2,221
  • 1
  • 13
  • 30
14
votes
2 answers

C++ is it possible to overload the unary minus operator of an rvalue reference?

is it possible to discern between these two methods? should one not mutate an rvalue when in this case seems perfectly reusable? TYPE a; TYPE b = -a; // unary operator- of a TYPE& aka lvalue ref TYPE c = -(a+b); // unary operator- of a TYPE&&…
14
votes
1 answer

Applications of const&& in range-for?

Are there any cases in which it does make sense to use const&& in range-for loops? for (const auto && x : c) // ?
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
14
votes
2 answers

std::bind and rvalue reference

Let's consider the following piece of code: class Widget{ }; int main(){ Widget w; auto lambda = bind([](Widget&& ref){ return; }, std::move(w)); return 0; } and it triggers error no match for call to…
Gilgamesz
  • 4,727
  • 3
  • 28
  • 63
14
votes
2 answers

How are rvalues in c++ stored in memory?

Trying to learn lvalues, rvalues and memory allocation for them. So with a lot of learning materials there is a bit of chaos. An rvalue is a value that needs to exist only in bounds of a expression where it was created (until C++11 at least). So it…
Il'ya Zhenin
  • 1,272
  • 2
  • 20
  • 31
14
votes
3 answers

How does std::forward receive the correct argument?

Consider: void g(int&); void g(int&&); template void f(T&& x) { g(std::forward(x)); } int main() { f(10); } Since the id-expression x is an lvalue, and std::forward has overloads for lvalues and rvalues, why doesn't the call…
template boy
  • 10,230
  • 8
  • 61
  • 97
14
votes
6 answers

C++0x rvalue references and temporaries

(I asked a variation of this question on comp.std.c++ but didn't get an answer.) Why does the call to f(arg) in this code call the const ref overload of f? void f(const std::string &); //less efficient void f(std::string &&); //more efficient void…
Doug
  • 8,780
  • 2
  • 27
  • 37
14
votes
2 answers

Should the assignment operator observe the assigned object's rvalueness?

For class types it is possible to assign to temporary objects which is actually not allowed for built-in types. Further, the assignment operator generated by default even yields an lvalue: int() = int(); // illegal: "expression is not…
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
14
votes
2 answers

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 #include #include std::string && Get_String(void); int main(){ std::string str{Get_String()}; …
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
14
votes
2 answers

Why do I need to use std::move in the initialization list of a move-constructor?

Let's say I have a (trivial) class, which is move-constructible and move-assignable but not copy-constructable or copy-assignable: class movable { public: explicit movable(int) {} movable(movable&&) {} movable& operator=(movable&&) {…
sarnesjo
  • 6,054
  • 2
  • 20
  • 17
14
votes
4 answers

c++ unique_ptr argument passing

Suppose I have the following code: class B { /* */ }; class A { vector vb; public: void add(B* b) { vb.push_back(b); } }; int main() { A a; B* b(new B()); a.add(b); } Suppose that in this case, all raw pointers B* can be…
Bérenger
  • 2,678
  • 2
  • 21
  • 42
13
votes
1 answer

What is the type of a named rvalue reference?

Consider the following code: int&& x = 42; static_assert(std::is_same::value, "&&"); static_assert(std::is_same::value, "&" ); So, what is the type of x? Is it an int&& or an int&? (I asked myself this…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
13
votes
2 answers

C++0x: rvalue reference versus non-const lvalue

When programming in C++03, we can't pass an unnamed temporary T() to a function void foo(T&);. The usual solution is to give the temporary a name, and then pass it like: T v; foo(v); Now, along comes C++0x - and now with rvalue references, a…
Channel72
  • 24,139
  • 32
  • 108
  • 180