Questions tagged [rvalue]

An rvalue is a temporary object (or subobject) or is a value not directly associated with an object.

Traditionally known as r-values since they generally appear on the right hand side of an expression; rvalues are temporary object whose lifetime does not extend past the current expression (unless bound to a an appropriate reference see ).

An rvalue is not directly associated with an object. Often a simple test is applied to determine if is an rvalue; "Is it named?"; if so, it is not an rvalue.

C++11 extended the original definition of an rvalue to include prvalues (pure rvalues) and xvalues (expiring values).

For a more general post on value categories, see this post on SO.

718 questions
13
votes
4 answers

Array and Rvalue

$4.2/1 - "An lvalue or rvalue of type “array ofN T” or “array of unknown bound of T” can be converted to an rvalue of type “pointer to T.” The result is a pointer to the first element of the array." I am not sure how do we get an rvalue…
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
13
votes
2 answers

Should implicitly generated assignment operators be & ref-qualified?

The following code compiles without problem on gcc 4.8.1: #include struct foo { }; int main() { foo bar; foo() = bar; foo() = std::move( bar ); } It seems the implicitly generated assignment operators for foo are not &…
Andrew Durward
  • 3,771
  • 1
  • 19
  • 30
13
votes
3 answers

Pass lvalue to rvalue

I made a small 'blocking queue' class. It irritates me that I have created redundant code for values passed into the enqueue member function. Here are the two functions that do the same exact thing (except the rvalue uses std::move to move the…
TheAJ
  • 10,485
  • 11
  • 38
  • 57
12
votes
2 answers

C++ rvalue parameter

I write this code: #include using namespace std; class Foo { public: int a = 0; Foo() { cout << "ctor: " << this << endl; } ~Foo() { cout << "dtor: " << this << endl; } }; Foo f() { Foo foo; …
philquinn
  • 131
  • 3
12
votes
2 answers

Why is an enum variable an rvalue here?

Example: typedef enum Color { RED, GREEN, BLUE } Color; void func(unsigned int& num) { num++; } int main() { Color clr = RED; func(clr); return 0; } I get the following error when I compile this: : In function…
Koen
  • 311
  • 2
  • 11
12
votes
2 answers

Understanding the warning: binding r-value to l-value reference

I want to pass a struct by reference so it won't be copied, but Resharper is giving the warning below: struct sometype { }; sometype foo() { sometype x; return x; } void bar() { sometype & a = foo();//Binding r-value to l-value…
shinzou
  • 5,850
  • 10
  • 60
  • 124
12
votes
1 answer

Passing rvalue reference to const lvalue reference paremeter

I am trying to understand C++11 rvalue references and how to use them for optimal performance in my code. Let's say we have a class A that has a member pointer to a large amount of dynamically allocated data. Furthermore, a method foo(const A& a)…
Sven
  • 143
  • 1
  • 1
  • 7
12
votes
1 answer

Understanding template argument deduction with rvalue/lvalue

This is a followup from function template does not recognize lvalue Lets play with the following code: #include template void func(T&&) { std::cout<<"in rvalue\n"; } template void func(const T&) { std::cout<<"in…
hivert
  • 10,579
  • 3
  • 31
  • 56
12
votes
3 answers

swap temporary tuples of references

I'm writing a custom iterator that, when dereferenced returns a tuple of references. Since the tuple itself is ephemeral, I don't think I can return a reference from operator*(). I think my iterator makes sense semantically, since it has reference…
Ben Jones
  • 919
  • 1
  • 8
  • 22
12
votes
2 answers

Exact difference between rvalue and lvalue

While I was reading http://thbecker.net/articles/rvalue_references/section_01.html, I got following snippiest. // lvalues: // int i = 42; i = 43; // ok, i is an lvalue int& foo(); foo() = 42; // ok, foo() is an lvalue int* p1 = &foo(); // ok, foo()…
Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
12
votes
7 answers

where is rvalue stored in c?

in C, i have this code piece: int a; a = 10 + 5 - 3 I want to ask: where is (10+5-3) stored at? (As far as I know, a is located on stack, how about (10+5-3)? How does this rvalue get calculated?)
user188276
11
votes
1 answer

Are compilers clever enough to std::move variables going out of scope?

Consider the following piece of code: std::vector Foo() { std::vector v = Bar(); return v; } return v is O(1), since NRVO will omit the copy, constructing v directly in the storage where the function's return value would otherwise…
André Harder
  • 354
  • 2
  • 11
11
votes
2 answers

What is "Extending move semantics to *this" all about?

Please, could someone explain in plain English what is "Extending move semantics to *this"? I am referring to this proposal. All what am looking for is what is that & why do we need that. Note that I do understand what an rvalue reference is in…
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
11
votes
2 answers

rvalue on the left side

Why is this code compiling? I thought that rvalues returned by ctor are not located in memory and therefore can't be used as lvalues. #include #include class Y { public : explicit Y(size_t num = 0) : m_resource…
John Difool
  • 5,572
  • 5
  • 45
  • 80
11
votes
1 answer

Aren't elements of a temporary array rvalues themselves?

using intArray = int[]; int (&a) [4] = intArray{1, 2, 3, 4}; This is not allowed since it's illegal to bind a non-const lvalue reference to a temporary (rvalue). Both g++ 4.9.1 and clang 3.4.2 barks back with errors; it compiles fine when a is…
legends2k
  • 31,634
  • 25
  • 118
  • 222