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
19
votes
4 answers

Is it possible to obtain the address of the 'this' pointer?

I read that this is an rvalue and we cannot get its address by applying &this. In my code, I have tried to use a reference binding to this. I'm wondering which way will give the address of this? Or are both wrong? What exactly is this? An lvalue, an…
Range Hao
  • 211
  • 2
  • 5
19
votes
4 answers

Why does as_const forbid rvalue arguments?

I wanted to ask why as_const forbids rvalue arguments, according to cppreference.com (i.e. why the Standards folks made it so, not why cppreference.com specifically quoted them on that. And also not where in the spec the intent of the committee is…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
19
votes
4 answers

Const reference and lvalue

We cannot write int& ref = 40 because we need lvalue on right side. But we can write const int& ref = 40 . Why is this possible? 40 is rvalue instead lvalue I know that this is an exception but why?
NewB
  • 344
  • 3
  • 12
19
votes
2 answers

What is an example of a difference in allowed usage or behavior between an xvalue and a prvalue FOR NON-POD objects?

What are rvalues, lvalues, xvalues, glvalues, and prvalues? gives a good overview of the taxonomy of rvalues/lvalues, and one of the recent answers to that question (https://stackoverflow.com/a/9552880/368896) stresses the point that prvalues are…
Dan Nissenbaum
  • 13,558
  • 21
  • 105
  • 181
19
votes
1 answer

What is the value category of the operands of C++ operators when unspecified?

PREMISE: The C++11 Standard classifies expressions into three disjoint value categories: lvalues, xvalues, and prvalues (§ 3.10/1). An explanation of what value categories are is available for instance here. I am struggling to figure out what are…
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
18
votes
1 answer

How can a returned object be assignable?

In Effective C++, Item 3, Scott Meyers suggests overloading operator* for a class named Rational: class Rational { ... }; const Rational operator*(const Rational& lhs, const Rational& rhs); The reason for the return value being…
qdii
  • 12,505
  • 10
  • 59
  • 116
18
votes
4 answers

prolonging the lifetime of temporaries

What is the design rationale behind allowing this const Foo& a = function_returning_Foo_by_value(); but not this Foo& a = function_returning_Foo_by_value(); ? What could possible go wrong in the second line (which would not already go wrong in the…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
18
votes
1 answer

Why does std::forward have two overloads?

Given the following reference collapsing rules T& & --> T& T&& & --> T& T& && --> T& T&& && --> T&& The third and fourth rule imply that T(ref qualifer) && is the identity transformation, i.e. T& stays at T& and T&& stays at T&&. Why do we have…
Curious
  • 20,870
  • 8
  • 61
  • 146
18
votes
2 answers

Why is the result of "decltype(i+j)" not an rvalue reference?

I'm trying to come up a simple example for an operation that results in a rvalue. This test case should have worked, but surprisingly (to me), the result of adding two ints is not an rvalue (reference). What am I missing here? void test(int i, int…
anderas
  • 5,744
  • 30
  • 49
18
votes
5 answers

Why is T() = T() allowed?

I believe the expression T() creates an rvalue (by the Standard). However, the following code compiles (at least on gcc4.0): class T {}; int main() { T() = T(); } I know technically this is possible because member functions can be invoked on…
18
votes
3 answers

On how to recognize Rvalue or Lvalue reference and if-it-has-a-name rule

I was reading Thomas Becker's article on rvalue reference and their use. In there he defines what he calls if-it-has-a-name rule: Things that are declared as rvalue reference can be lvalues or rvalues. The distinguishing criterion is: if it has a…
Mustafa
  • 1,814
  • 3
  • 17
  • 25
17
votes
3 answers

Binding rvalue to const lvalue reference

For some reason I didn't manage to find this exact question. Why is it allowed to bind an rvalue to const lvalue reference, although it is impossible to to the same without the const? I do understand that the lifetime of the rvalue gets an extension…
Eliran Abdoo
  • 611
  • 6
  • 17
16
votes
1 answer

Is there a convenience constructor in C++?

Is it possible for an overloaded constructor to somehow call another constructor within the class, similar to the code below? class A { public: A(std::string str) : m_str(str) {} A(int i) { *this = std::move(A(std::to_string(i))); } …
Makaronodentro
  • 907
  • 1
  • 7
  • 21
16
votes
2 answers

dynamic_cast and rvalue reference

class A{ public: virtual ~A() {}; }; class B : public A{ }; int main(){ A&& p = B(); dynamic_cast(std::move(p)); } Throws the error (g++ 5.2.0): error: conversion to non-const reference type 'std::remove_reference::type&…
SergeantPenguin
  • 843
  • 7
  • 16
16
votes
4 answers

Why does std::reference_wrapper not accept a temporary?

Normally, rvalues can bind to const references (const SomeType&). It's built into the language. However, std::reference_wrapper does not accept an rvalue as its constructor argument since the corresponding overload is deliberately deleted.…
isarandi
  • 3,120
  • 25
  • 35
1 2
3
47 48