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
16
votes
5 answers

Correct way to return an rvalue reference to this

The code below results in Undefined Behaviour. Be sure to read ALL the answers for completeness. When chaining an object via the operator<< I want to preserve the lvalue-ness / rvalue-ness of the object: class Avenger { public: Avenger&…
bolov
  • 72,283
  • 15
  • 145
  • 224
15
votes
5 answers

What kind of syntactic sugar is available in Perl to reduce code for l/rvalue operators vs. if statements?

There's a bunch out there, as Perl is a pretty sugary language, but the most used statements in any language is the combination of if statements and setting values. I think I've found many of them, but there's still a few gaps. Ultimately, the…
SineSwiper
  • 2,046
  • 2
  • 18
  • 22
15
votes
2 answers

I think I may have come up with an example of rvalue of array type

C++03 §4.2 N°1: An lvalue or rvalue of type “array of N 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. What has been confusing in this statement…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
15
votes
2 answers

Why const for implicit conversion?

After extensive reading of ISO/IEC 14882, Programming language – C++ I'm still unsure why const is needed for implicit conversion to a user-defined type with a single argument constructor like the following #include class X { public: …
Brandon Cook
  • 1,362
  • 11
  • 11
15
votes
1 answer

Why does std::forward return static_cast and not static_cast?

Let's have a function called Y that overloads: void Y(int& lvalue) { cout << "lvalue!" << endl; } void Y(int&& rvalue) { cout << "rvalue!" << endl; } Now, let's define a template function that acts like std::forward template void f(T&&…
gedamial
  • 1,498
  • 1
  • 15
  • 30
15
votes
3 answers

Why is overloading on just one ref-qualifier not allowed?

Apparently, overloading on ref-qualifiers is not allowed – this code won't compile if you remove either & or && (just the tokens, not their functions): #include struct S { void f() & { std::cout << "Lvalue" << std::endl; } void…
Leo Heinsaar
  • 3,887
  • 3
  • 15
  • 35
15
votes
3 answers

C++ range-based for loop over valarray rvalue is not working

I would like to iterate over a temporary valarray, but it isn't working. Here is my (non-working) code: #include #include int main() { using namespace std; valarray numerators = {99, 26, 25}; …
Jasha
  • 5,507
  • 2
  • 33
  • 44
14
votes
5 answers

What is decltype(0 + 0)?

(Prompted by an answer.) Given N3290, §7.1.6.2p4, where the list items are unnumbered, but numbered here for our convenience: The type denoted by decltype(e) is defined as follows: if e is an unparenthesized id-expression or an unparenthesized…
Fred Nurk
  • 13,952
  • 4
  • 37
  • 63
14
votes
1 answer

Classes, Rvalues and Rvalue References

An lvalue is a value bound to a definitive region of memory whereas an rvalue is an expression value whose existence is temporary and who does not necessarily refer to a definitive region of memory. Whenever an lvalue is used in a position in which…
No One
  • 143
  • 1
  • 5
14
votes
1 answer

Why rvalue reference binding to xvalue doesn't work in my code?

I tried to understand lvalue and rvalue in C++11. So I wrote a test code: int x = 10; int foo() { return x; } int& bar() { return x; } int&& baz() { return 10; } int main() { int& lr1 = 10; // error: lvalue references rvalue int& lr2 =…
Seokmin Hong
  • 612
  • 1
  • 5
  • 15
14
votes
7 answers

Why is taking the address of a temporary illegal?

I know that the code written below is illegal void doSomething(std::string *s){} int main() { doSomething(&std::string("Hello World")); return 0; } The reason is that we are not allowed to take the address of a temporary object. But my…
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
14
votes
2 answers

How are rvalues in c++ stored in memory?

Trying to learn lvalues, rvalues and memory allocation for them. So with a lot of learning materials there is a bit of chaos. An rvalue is a value that needs to exist only in bounds of a expression where it was created (until C++11 at least). So it…
Il'ya Zhenin
  • 1,272
  • 2
  • 20
  • 31
13
votes
2 answers

What rvalues have names?

@FredOverflow mentioned in the C++ chatroom that this is a rare case of rvalues that have names. The C++0x FDIS mentions under 5.1.1 [expr.prim.general] p4: Otherwise, if a member-declarator declares a non-static data member (9.2) of a class X, the…
Xeo
  • 129,499
  • 52
  • 291
  • 397
13
votes
1 answer

Why does std::shuffle take an rvalue reference?

Since C++11, std::shuffle() takes an rvalue reference to a random bit generator: template void shuffle(RandomIt first, RandomIt last, URBG&& g); And so I might call it thus: std::vector v = {...}; std::random_device…
jma
  • 3,580
  • 6
  • 40
  • 60
13
votes
1 answer

Is a data member of a temporary object an xvalue in C++11?

#include using namespace std; struct A { vector coll; }; void f(const vector&){} void f(vector&&){} int main() { f(A().coll); // Is "A().coll" an xvalue? } Does C++11 guarantee f(A().coll) will call void…
xmllmx
  • 39,765
  • 26
  • 162
  • 323