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

What is the operations or treatments we can do on xvalues and not on prvalues and vice versa

I'm wondering why rvalue expressions are subdivided in two groups (xvalues and prvalues). Saying differently is there a lot of place where the treatment on rvalues is different according to the fact the expression is either an xvalue or a prvalue?…
Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145
0
votes
1 answer

Do I need to use std::move again?

For below code, I want to use the std::move to improve the efficiency. I have two functions, the first function uses std::move, and the second function just calls the first function. So, do I need to use std::move again in the function "vector…
Michael
  • 673
  • 2
  • 5
  • 23
0
votes
3 answers

Using the result of compound assignment as an lvalue

I'm surprised that this works: double x = 3; double y = 2; (x *= 2) += y; std::cout << x << std::endl; The result is 8, which is what it looks like the programmer is trying to achieve. But I thought assignment operators returned an rvalue - how…
Tom
  • 7,269
  • 1
  • 42
  • 69
0
votes
1 answer

What's this code supposed to do? (reference to rvalue)

I have read that the code below is valid in C++11: int && a = 3; a = 4; Is it supposed to write 4 in the memory address where the numeric literal 3 is stored? Maybe some compiler optimizations would prevent this from happening, but is it supposed…
jbgs
  • 2,795
  • 2
  • 21
  • 28
0
votes
1 answer

php include as rvalue?

I wonder if it is possible to use php include as a Rvalue, like this example: $foo = include('bar.php); I did a few experiments but it seems to not be possible. The php files that I would like to load in this way are cached dumps of objects and…
fstab
  • 4,801
  • 8
  • 34
  • 66
0
votes
3 answers

Returning a mutable value in C++11

I was wondering about having a method return an r-value. Specifically, I was wondering if there was a way to do this with an overloaded operator. I have this code: struct vec4 { float x; float y; float z; float w; ... inline…
jepugs
  • 445
  • 4
  • 10
0
votes
2 answers

How lvalue can be converted as rvalue

int x = 8; int y = x ; Here how a lvalue can be act as rvalue ? I know this is a silly question , but i just want to make my concepts clear on rvalue and lvalue .
Viku
  • 2,845
  • 4
  • 35
  • 63
0
votes
5 answers

Return values in c++03 vs 11

I have spend a few hours about rvalue s and lvalue. Here is what I understand int main() { //..... Foo foo = Bar1(); foo = Bar2(); //...... } Foo Bar1() { //Do something including create foo return foo; } Foo& Bar2() { //Do…
aiao
  • 4,621
  • 3
  • 25
  • 47
0
votes
2 answers

Convert a Vector of Strings to the Rvalue of an Assignement Statement

I cant think of an efficient way to convert a vector of strings to become the rvalue of an assignment statement. So for example, i have a vector with three elements "5", "*", "3" so what I need is an assignment statement that is basically equivalent…
Jeanno
  • 2,769
  • 4
  • 23
  • 31
0
votes
2 answers

return *this; deletes pointers

What i think is occuring is that the rvalue A returned by SetVar is an identical copy to Class and shares the same pointer Var. but when the rvalue calls its deconstructor it deletes Class's Val. class A { private: int* Var; public: A…
Lauer
  • 517
  • 1
  • 6
  • 11
0
votes
2 answers

Is it a good practice that declaring a function as BigStruct&& foo(...);

class A { public: A() { cout << "A()" << endl; } A(const A&) { cout << "A(const A&)" << endl; } A(A&&) { cout << "A(A&&)" << endl; } A& operator=(const A&) { cout <<…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
-1
votes
1 answer

Non-member operator: cannot bind non-const lvalue reference to an rvalue

Binding a non-const rvalue to a rvalue with member operators work (case A), but binding to a non-member operator (case B) does not: struct A { A & operator<<(int i) { return *this; } }; struct B { }; inline B & operator<<(B & b, int i) { return…
hpc64
  • 35
  • 5
-1
votes
1 answer

Why std::move doesn't avoid a second destruction?

I know ordinary std::vector::push_back() will copy the object. I hope this code would only destruct a only once, using std::move() and A(A&&) noexcept to avoid copying. But it doesn't seem to work. Is there any way that I can construct an object…
-1
votes
1 answer

Deallocating resources through move semantics

I came across codebase, where "moving ownership" through move semantic is used very frequently to deallocate resources. Example: void process_and_free(Foo&& arg) { auto local_arg = std::move(arg); ... // Use local_arg } void caller() { …
Marek
  • 79
  • 1
  • 6
-1
votes
1 answer

Is temporary object initialization expresssion implicitly converted to xvalue while we access the this pointer?

The code is: class A { public: void f() { cout << this; } }; int main() { A{}; // prvalue A{}.f(); // Is A{} in here converted to xvalue? } On https://en.cppreference.com/w/cpp/language/implicit_conversion, I learned the Temporary…
colin
  • 49
  • 2