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
11
votes
1 answer

C++ type cast operator code that won't compile in visual studio 2012, but worked fine in visual studio 2005

I'm trying to update a old project that has been building with visual studio 2005 to uses visual studio 2012, and I'm getting an error that I cant solve. The code that works fine under VS2005: #include #include #include…
Robert Sjödahl
  • 734
  • 5
  • 19
11
votes
2 answers

Why are tokens wrapped by parentheses not r-value expressions?

Consider the following code: #include struct Foo { Foo() : bar( 0 ) {} int bar; }; int main() { Foo foo; ++(foo.bar); std::cout<< foo.bar << std::endl; system("pause"); return 0; }; Why does foo.bar evaluate to…
Martin85
  • 308
  • 4
  • 13
11
votes
2 answers

Result of ternary operator not an rvalue

If you compile this program with a C++11 compiler, the vector is not moved out of the function. #include using namespace std; vector create(bool cond) { vector a(1); vector b(2); return cond ? a : b; } int main()…
Pait
  • 757
  • 6
  • 19
10
votes
4 answers

One VS2010 bug ? Allowing binding non-const reference to rvalue WITHOUT EVEN a warning?

string foo() { return "hello"; } int main() { //below should be illegal for binding a non-const (lvalue) reference to a rvalue string& tem = foo(); //below should be the correct one as only const reference can be bind to…
RoundPi
  • 5,819
  • 7
  • 49
  • 75
10
votes
1 answer

Why is my code printing rvalue 2 times instead of rvalue & lvalue?

So I wanted to practice the usage of std::forward and created a Test class with 2 constructors. 1 with T& and the other with T&& as overload. T& prints lvalue, and T&& prints rvalue so I know which one of the constructors is being used. I create 2…
3l4x
  • 103
  • 4
10
votes
2 answers

xvalues: differences between non class types and class types

Consider the minimal example below: #include struct S { }; int main() { S s; std::move(s) = S{}; } It compiles with no errors. If I use non class types instead, I get an error. As an example, the code below doesn't…
skypjack
  • 49,335
  • 19
  • 95
  • 187
10
votes
2 answers

difference between rvalue reference and lvalue reference as argument

After reading the post:http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html. I can not figure out that when you write functions that take lvalue or rvalue references as arguments, such as this: void printReference…
sydridgm
  • 1,012
  • 4
  • 16
  • 30
10
votes
2 answers

Why does std::move take a forward reference?

The implementation of std::move basically looks like this: template typename std::remove_reference::type&& move(T&& t) { return static_cast::type&&>(t); } Note that the parameter of std::move is…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
10
votes
5 answers

Reference initialization in C++

Can anybody explain to me why there is a difference between these two statements? class A{}; const A& a = A(); // correct A& b = A(); // wrong It says invalid initialization of non-const reference of type A& from a…
skydoor
  • 25,218
  • 52
  • 147
  • 201
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
9
votes
5 answers

Why don't rvalues have an address?

Why don't rvalues have a memory address? Are they not loaded into the RAM when the program executes or does it refer to the values stored in processor registers?
user746341
  • 127
  • 1
  • 5
9
votes
4 answers

lvalue required as increment operand error

#include int main() { int i = 10; printf("%d\n", ++(-i)); // <-- Error Here } What is wrong with ++(-i)? Please clarify.
Wei
  • 93
  • 1
  • 3
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
9
votes
3 answers

Dereference a rvalue shared_ptr

I'm using a library which export a function like: // there is some type T std::shared_ptr foo(params); and while the following code works fine: auto p = foo(params); auto & v0 = *p; // use v0 as a T's reference the below crashes: auto & v1 =…
Ta Thanh Dinh
  • 638
  • 1
  • 5
  • 12
9
votes
2 answers

Returning a member from an rvalue object

Lets take two structs/classes struct C1{ C1(){}; C1(C1&){std::cout<<"copy"<