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

Passing vector with std::move function signature

Consider the code below void foo(std::vector v) { //do something here } //calling the function vector v1 = {1,2,3}; foo(std::move(v1)); My question is, isn't the function foo supposed to have signature void foo(std::vector&& v)…
Kraken
  • 23,393
  • 37
  • 102
  • 162
9
votes
2 answers

Rvalue reference parameters and template functions

If I define a function which accepts an rvalue reference parameter: template void fooT(T &&x) {} I can call it, using GCC 4.5, with either a, ar, or arr: int a, &ar = a, &&arr = 7; fooT(a); fooT(ar); fooT(arr); However, calling a…
user2023370
  • 10,488
  • 6
  • 50
  • 83
9
votes
4 answers

Compiler deduction of rvalue-references for variables going out of scope

Why won't the compiler automatically deduce that a variable is about to go out of scope, and therefore let it be considered an rvalue-reference? Take for example this code: #include int foo(std::string && bob); int foo(const std::string &…
jonesmz
  • 427
  • 3
  • 6
9
votes
3 answers

How is it possible to get a reference to an rvalue?

I have used std::move and std::forward in C++. My question is: how are these functions actually implemented by the standard library? If an lvalue is something you can get the address of, and an rvalue is exclusively not an lvalue, how can you…
user3816764
  • 489
  • 5
  • 22
9
votes
3 answers

Some clarification on rvalue references

First: where are std::move and std::forward defined? I know what they do, but I can't find proof that any standard header is required to include them. In gcc44 sometimes std::move is available, and sometimes its not, so a definitive include…
Dennis Zickefoose
  • 10,791
  • 3
  • 29
  • 38
9
votes
1 answer

rvalue reference to function

typedef void(&&RF)(void* p); RF rf() { return f; } int ay[10] = { 0 }; typedef int(&&RA)[10]; RA ra() { return ay; // error } cout << is_lvalue_reference::value << endl; // 1 The C++ reference says "rvalue references to…
Leonhart Squall
  • 810
  • 1
  • 7
  • 15
9
votes
1 answer

C++ universal reference in constructor and return value optimization (rvo)

Why does rvalue optimization not occur in classes with constructor with universal reference arguments? http://coliru.stacked-crooked.com/a/672f10c129fe29a0 #include template struct C { template
tower120
  • 5,007
  • 6
  • 40
  • 88
9
votes
2 answers

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

Here is some example code: #include class Foo { public: explicit Foo(int x) : data(x) {}; Foo& operator++() { data += 1; return *this; } void *get_addr() { return (void*)this; } friend Foo operator + (const…
CppNoob
  • 2,322
  • 1
  • 24
  • 35
9
votes
2 answers

Overload resolution with universal references

I have a function which can accept any type by universal reference, and would like to overload it for specific types (some of which are templated themselves, although I don't think that's important here). Unfortunately I can't quite seem to to get…
jleahy
  • 16,149
  • 6
  • 47
  • 66
9
votes
2 answers

Numeric vector operator overload+ rvalue reference parameter

I have the numeric vector template class below (vector for numerical calculations). I am trying make it possible to write D=A+B+C where all variables are Vector objects. A, B and C should not be modified. My idea is to use Vector operator+(Vector&&…
Lars Chr
  • 947
  • 1
  • 9
  • 12
9
votes
3 answers

r-value reference return type semantics?

Does a return type like this represent something meaningful in c++11? template R&& grabStuff(); T instance = grabStuff(); I would hope that grabStuff should throw a compile-time error if R does not have a move constructor, since…
lurscher
  • 25,930
  • 29
  • 122
  • 185
8
votes
1 answer

Are temporary objects xvalues?

I am currently writing my degree dissertation and it involves also some explaining of the theory behind C++11 which is really fine as C++ is my programming language of choice and the standard is more or less available for free (N3337) to get…
khaos
  • 632
  • 3
  • 11
8
votes
2 answers

Is an xvalue's lifetime extended when it is bound to a const lvalue reference?

If I write the following code: #include using namespace std; int main() { cout << &(int &&)123 << endl; return 0; } Then g++ complains: foo.cc: In function ‘int main()’: foo.cc:7:20: error: taking address of xvalue (rvalue…
FatalError
  • 52,695
  • 14
  • 99
  • 116
8
votes
2 answers

rvalue binding confusion in C++

I have three function calls that I think should be treated (about) the same, but clearly they are not. I'm trying to understand why one of three doesn't compile (g++ -std=c++0x). // Minimal example to reproduce a compile bug I want to…
jma
  • 3,580
  • 6
  • 40
  • 60
8
votes
2 answers

Return rvalue reference or temporary object in C++11

In Effective Modern C++ item 12, there is a sample code about the C++11 functions' reference qualifiers: class Widget { public: using DataType = std::vector; … DataType& data() & // for lvalue Widgets { return…
sfzhang
  • 669
  • 9
  • 18