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

Why pre-increment operator gives rvalue in C?

In C++, pre-increment operator gives lvalue because incremented object itself is returned, not a copy. But in C, it gives rvalue. Why?
Happy Mittal
  • 3,667
  • 12
  • 44
  • 60
31
votes
6 answers

Is a member of an rvalue structure an rvalue or lvalue?

A function call returning a structure is an rvalue expression, but what about its members? This piece of code works well with my g++ compiler, but gcc gives a error saying "lvalue required as left operand of assignment": struct A { int…
hpsMouse
  • 2,004
  • 15
  • 20
31
votes
3 answers

Are literal strings and function return values lvalues or rvalues?

Just wonder if a literal string is an lvalue or an rvalue. Are other literals (like int, float, char etc) lvalue or rvalue? Is the return value of a function an lvalue or rvalue? How do you tell the difference?
Tim
  • 1
  • 141
  • 372
  • 590
30
votes
1 answer

Best form for constructors? Pass by value or reference?

I'm wondering the best form for my constructors. Here is some sample code: class Y { ... } class X { public: X(const Y& y) : m_y(y) {} // (a) X(Y y) : m_y(y) {} // (b) X(Y&& y) : m_y(std::forward(y)) {} // (c) Y m_y; } Y f() { return…
Clinton
  • 22,361
  • 15
  • 67
  • 163
27
votes
2 answers

C++0x const RValue reference as function parameter

I am trying to understand why someone would write a function that takes a const rvalue reference. In the code example below what purpose is the const rvalue reference function (returning "3"). And why does overload resolution preference the const…
MW_dev
  • 2,146
  • 1
  • 26
  • 40
26
votes
2 answers

Can an optimizing compiler add std::move?

Can a compiler do automatic lvalue-to-rvalue conversion if it can prove that the lvalue won't be used again? Here's an example to clarify what I mean: void Foo(vector values) { ...} void Bar() { vector my_values {1, 2, 3}; …
Edward Loper
  • 15,374
  • 7
  • 43
  • 52
25
votes
5 answers

Why are literals and temporary variables not lvalues?

I've read that lvalues are "things with a defined storage location". And also that literals and temporaries variables are not lvalues, but no reason is given for this statement. Is it because literals and temporary variables do not have defined…
pasha
  • 2,035
  • 20
  • 34
25
votes
2 answers

Does copy elision work with structured bindings

Does mandatory copy elision apply to decomposition via structured bindings? Which of the following cases does that apply to? // one auto [one, two] = std::array{SomeClass{1}, SomeClass{2}}; // two auto [one, two] =…
Curious
  • 20,870
  • 8
  • 61
  • 146
25
votes
3 answers

Why are rvalues references variables not rvalue?

Let's say I have two overloads of a function f void f(T&&); // #1 void f(T&); // #2 Then in the body of another function g void g(T&& t) { f(t); // calls #2 } the overload f(T&) will be called because t is considered an lvalue. This is very…
ElefEnt
  • 2,027
  • 1
  • 16
  • 20
23
votes
2 answers

C++03. Test for rvalue-vs-lvalue at compile-time, not just at runtime

In C++03, Boost's Foreach, using this interesting technique, can detect at run-time whether an expression is an lvalue or an rvalue. (I found that via this StackOverflow question: Rvalues in C++03 ) Here's a demo of this working at run-time (This is…
Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
23
votes
2 answers

structured bindings with std::minmax and rvalues

I ran into a rather subtle bug when using std::minmax with structured bindings. It appears that passed rvalues will not always be copied as one might expect. Originally I was using a T operator[]() const on a custom container, but it seems to be the…
Zulan
  • 21,896
  • 6
  • 49
  • 109
22
votes
3 answers

PODs, non-PODs, rvalue and lvalues

Could anyone explain the details in terms of rvalues, lvalues, PODs, and non-PODs the reason why the first expression marked below is not ok while the second expression marked below is ok? In my understanding both int() and A() should be rvalues,…
anonymous
  • 231
  • 2
  • 4
21
votes
3 answers

r-value parameters in a function

I was wondering about a c++ behaviour when an r-value is passed among functions. Look at this simple code: #include void foo(std::string&& str) { // Accept a rvalue of str } void bar(std::string&& str) { // foo(str); // Does…
BiagioF
  • 9,368
  • 2
  • 26
  • 50
20
votes
2 answers

Why are C++0x rvalue reference not the default?

One of the cool new features of the upcoming C++ standard, C++0x, are "rvalue references." An rvalue reference is similar to an lvalue (normal) reference, except that it can be bound to a temporary value (normally, a temporary can only be bound to a…
Zifre
  • 26,504
  • 11
  • 85
  • 105
20
votes
1 answer

Is it legal to move the .str() member of a stringstream?

Consider the following example: #include template inline std::string to_string(T const & op) { std::ostringstream result; result << op; return result.str(); } If I was to return result, instead of result.str() it…
kamikaze
  • 1,529
  • 8
  • 18
1
2
3
47 48