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
10
votes
3 answers

non-const reference of type from an rvalue

Consider the following code: class Widget{}; template T &&foo2(T &&t){ return std::forward( t ); } /// Return 1st element template typename std::tuple_element<0, typename std::decay::type >::type &&foo(T &&t){ …
tower120
  • 5,007
  • 6
  • 40
  • 88
10
votes
3 answers

Why can't I pass an rvalue-reference as it is to another function in C++11?

I have a code: void f(int&& i) { auto lambda = [](int&& j) { (void)j; } lambda(i); } int main() { f(5); } Clang++ gives an error: no known conversion from 'int' to 'int &&' for 1st argument Why the i changes its type to int when being passed…
abyss.7
  • 13,882
  • 11
  • 56
  • 100
10
votes
1 answer

Temporary const array not binding to rvalue reference

I have the following test program: #include #include #include template void foo(Ty (&&)[N]) { std::cout << "Ty (&&)[" << N << "]\t" << std::is_const::value <<…
Simple
  • 13,992
  • 2
  • 47
  • 47
10
votes
3 answers

Do I use std::forward or std::move here?

Let's say I have: template struct NodeBase { T value; NodeBase(T &&value) : value(value) { } }; and I inherit from it: template struct Node : public NodeBase { Node(T &&value) : NodeBase(…
user541686
  • 205,094
  • 128
  • 528
  • 886
10
votes
3 answers

Pass by value or rvalue-ref

For move enabled classes is there a difference between this two? struct Foo { typedef std::vector Vectype; Vectype m_vec; //this or void bar(Vectype&& vec) { m_vec = std::move(vec); } //that void bar(Vectype vec) { m_vec =…
hurcan solter
  • 127
  • 1
  • 10
10
votes
1 answer

Difference between "return-by-rvalue-ref" & "return-by-value" when you return using std::move?

Considering the following code: #include using namespace std; struct I { I(I&& rv) { cout << "I::mvcotr" << endl; } }; struct C { I i; I&& foo() { return move(i) }; } }; int main() { C c; I i = c.foo(); } C…
Benji Mizrahi
  • 2,154
  • 2
  • 23
  • 38
10
votes
2 answers

Rvalue reference: Why aren't rvalues implicitly moved?

On Artima article about C++ rvalue reference (http://www.artima.com/cppsource/rvalue.html) there is words: That's why it is necessary to say move(x) instead of just x when passing down to the base class. This is a key safety feature of move…
demi
  • 5,384
  • 6
  • 37
  • 57
10
votes
2 answers

Extension of the lifetime of a temporary with an rvalue reference

According to another answer, an rvalue reference will not extend the lifetime of a temporary if the expression referring to it is an xvalue expression. Since std::move returns an rvalue reference, the expression of calling it is an xvalue and so the…
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
10
votes
2 answers

c++11 move insertion for std::deque or std::list

I understand reasonably well how rvalue references work, but I'm not exactly sure how they work with iterators in the STL. Here's something that I want: void insertList(std::list& L, std::list&& R, std::list::iterator insertPoint) { …
rlbond
  • 65,341
  • 56
  • 178
  • 228
10
votes
1 answer

Overload ambiguity when passing R-value to function that takes L-value

I have 2 overloaded functions - one takes an L-value, and the other takes an R-value. The purpose is so that the function can be called like: Obj obj; foo(obj); OR: foo(Obj()); So, I write 2 overloaded functions: template void foo(T&…
Channel72
  • 24,139
  • 32
  • 108
  • 180
10
votes
4 answers

prevent pass-by-ref of temporary object

I have a class that 'remembers' a reference to some object (e.g. an integer variable). I can't have it reference a value that's destructed immediately, and I'm looking for a way to protect the users of my class from doing so by accident. Is an…
xtofl
  • 40,723
  • 12
  • 105
  • 192
10
votes
2 answers

std::thread with movable, non-copyable argument

The following program doesn't build in VS11 beta, gcc 4.5, or clang 3.1 #include #include int main() { std::unique_ptr p; std::thread th([](std::unique_ptr) { },std::move(p)); th.join(); } This is…
bames53
  • 86,085
  • 15
  • 179
  • 244
9
votes
4 answers

Using of rvalue references in c++11

I would like to implement a function that fills up a vector and then returns an rvalue reference. I tired something like: std::vector &&fill_list() { std::vector res; ... do something to fill res ... return res; } int main(int argc,…
Sambatyon
  • 3,316
  • 10
  • 48
  • 65
9
votes
2 answers

rvalue references in Visual Studio 2010

What are the differences between rvalue references as implemented in Visual Studio 2010 and as specified in the C++11? Are there any particular pitfalls to watch out for when using revalue references in Visual Studio 2010 that could make source…
wilx
  • 17,697
  • 6
  • 59
  • 114
9
votes
2 answers

Assign a value to an rvalue reference returned from function

#include template decltype(auto) index(Container &&arr, int n) { return std::forward(arr)[n]; } Make a function call : #include index(std::vector {1, 2, 3, 4, 5}, 2) = 0; When function calling…
Jonny0201
  • 433
  • 5
  • 10