Questions tagged [lvalue-to-rvalue]

Use this tag for lvalue to rvalue conversion questions

C++ expression is either an lvalue or an rvalue.

A lvalue refers to an object that persists beyond a single expression. You can think of an lvalue as an object that has a name. All variables, including nonmodifiable (const) variables, are lvalues.

A rvalue is a temporary value that does not persist beyond the expression that uses it.

Lvalues and Rvalues: https://msdn.microsoft.com/en-us/library/f90831hc.aspx

60 questions
50
votes
3 answers

When does lvalue-to-rvalue conversion happen, how does it work, and can it fail?

I see the term "lvalue-to-rvalue conversion" used in many places throughout the C++ standard. This kind of conversion is often done implicitly, as far as I can tell. One unexpected (to me) feature of the phrasing from the standard is that they…
orm
  • 2,835
  • 2
  • 22
  • 35
26
votes
1 answer

Shall structured binding be returned from a function as rvalue in C++20?

Consider a C++20 program where in function foo there is a structured binding auto [y]. The function returns y, which is converted in object of type A. A can be constructed either from const reference of from rvalue-reference. #include…
Fedor
  • 17,146
  • 13
  • 40
  • 131
19
votes
2 answers

Does the standard mandate an lvalue-to-rvalue conversion of the pointer variable when applying indirection?

TL;DR Given the following code: int* ptr; *ptr = 0; does *ptr require an lvalue-to-rvalue conversion of ptr before applying indirection? The standard covers the topic of lvalue-to-rvalue in many places but does not seem to specify enough…
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
18
votes
3 answers

Invalid initialization of non-const reference of type

In the following code, I'm not able to pass a temporary object as argument to the printAge function: struct Person { int age; Person(int _age): age(_age) {} }; void printAge(Person &person) { cout << "Age: " << person.age << endl; } int…
jeffreyveon
  • 13,400
  • 18
  • 79
  • 129
17
votes
2 answers

Does swap() cause undefined behaviour?

I'm trying to understand the conditions on std::swap from [C++11: utility.swap]. The template is defined as template void swap(T &, T &) (plus some noexcept details) and as having the effect of "exchanging the values stored at the two…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
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
13
votes
4 answers

Array and Rvalue

$4.2/1 - "An lvalue or rvalue of type “array ofN 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." I am not sure how do we get an rvalue…
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
12
votes
1 answer

Why doesn't C++ move construct rvalue references by default?

Say I have the following function void doWork(Widget && param) // param is an LVALUE of RRef type { Widget store = std::move(param); } Why do I need to cast param back to an rvalue with std::move()? Shouldn't it be obvious that the type of…
barney
  • 2,172
  • 1
  • 16
  • 25
11
votes
2 answers

rvalue on the left side

Why is this code compiling? I thought that rvalues returned by ctor are not located in memory and therefore can't be used as lvalues. #include #include class Y { public : explicit Y(size_t num = 0) : m_resource…
John Difool
  • 5,572
  • 5
  • 45
  • 80
8
votes
2 answers

rvalue binding confusion in C++

I have three function calls that I think should be treated (about) the same, but clearly they are not. I'm trying to understand why one of three doesn't compile (g++ -std=c++0x). // Minimal example to reproduce a compile bug I want to…
jma
  • 3,580
  • 6
  • 40
  • 60
8
votes
3 answers

Is `(i) = 1` illegal in standard C?

I'm writing a C compiler which follows this standard, and if I parse statements like this: int i; (i) = 1; my compiler will report an error which point out that (i) is a rvalue and should not be assignable. I checked the code and the rules, and…
user2269707
8
votes
1 answer

What is the significance of special language in standard for lvalue-to-rvalue conversions for unsigned character types of indeterminate value

In the C++14 standard (n3797), the section on lvalue to rvalue conversions reads as follows (emphasis mine): 4.1 Lvalue-to-rvalue-conversion [conv.lval] A glvalue (3.10) of a non-function, non-array type T can be converted to a prvalue. If T is an…
Chris Beck
  • 15,614
  • 4
  • 51
  • 87
7
votes
1 answer

Does lvalue-to-rvalue conversion ever happen to class types?

Practically every example of lvalue-to-rvalue conversion I've seen on the web relates to fundamental types like int etc. I couldn't find an example of l2r applicable to class types myself; in all the seemingly applicable examples there's usually a…
ledonter
  • 1,269
  • 9
  • 27
7
votes
1 answer

Undefined behavior inside void expressions

Is a C implementation required to ignore undefined behaviors occurring during the evaluation of void expressions as if the evaluation itself never took place? Considering C11, 6.3.2.2 §1: If an expression of any other type is evaluated as a void…
anol
  • 8,264
  • 3
  • 34
  • 78
7
votes
2 answers

Regarding lvalue-to-rvalue conversion, when is it required?

I've been reading quite many on the Internet and it seems that many people mentioned the following rules (but i couldn't find it in the standard), The addition operator + (and all other binary operators) requires both operands to be rvalue, and the…
user534498
  • 3,926
  • 5
  • 27
  • 52
1
2 3 4