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
7
votes
2 answers

Assigning Rvalue returned from function to another Rvalue

class Test { public: int n1; }; Test func() { return Test(); } int main() { func() = Test(); } This doesn't make sense to me. How and why is this allowed? Is it undefined behavior? If a function returns an rvalue, then how is…
Greg M
  • 954
  • 1
  • 8
  • 20
6
votes
1 answer

Unexpected output when invoking overloaded functions with different rvalue reference types

I'm encountering an unexpected behavior in my code when calling overloaded functions with different rvalue reference types. The code snippet below demonstrates the issue: #include void foo(int&& x) { std::cout << "int Rvalue: " << x…
6
votes
2 answers

Lvalue to Rvalue conversions

From 4.1/2 The value contained in the object indicated by the lvalue is the rvalue result. When an lvalue-to-rvalue conversion occurs within the operand of sizeof (5.3.3) the value contained in the referenced object is not accessed, since…
parc84
  • 257
  • 1
  • 2
  • 5
6
votes
2 answers

Can an lvalue at end of scope be treated as an rvalue?

EDIT: Consider 2 following examples: std::string x; { std::string y = "extremely long text ..."; ... x = y; // *** (1) } do_something_with(x); struct Y { Y(); Y(const Y&); Y(Y&&); ... // many "heavy" members }; struct…
4
votes
2 answers

Why the rvalue reference parameter cannot be passed between functions directly?

My code is as follows: #include using namespace std; class A{ public: void sendByRvalue(string&& str){ cout << str << endl; } }; class B{ private: A a; void send(string&& str){ a.sendByRvalue(str); …
4
votes
4 answers

Why an Rvalue Reference is Turned into Lvalue Reference by a Universal Reference

I suppose when a universal reference parameter is matched with an rvalue reference argument, an rvalue reference argument is returned. However, my testing shows that the rvalue reference is turned into a lvalue reference by the universal reference…
JavaMan
  • 4,954
  • 4
  • 41
  • 69
4
votes
1 answer

Understanding the example on lvalue-to-rvalue conversion

I have a hard time understanding how this code (an example from the C++14 draft standard [conv.lval]) invokes undefined behavior for g(false). Why does constexpr make the program valid? Also, what does it mean by "does not access y.n"? In both calls…
template boy
  • 10,230
  • 8
  • 61
  • 97
3
votes
5 answers

Will an lvalue to rvalue conversion happen?

C++ Standard (4/5) the lvalue-to-rvalue conversion is not done on the operand of the unary & operator. For example: int x; int *p = &x; In the above case, are p are &x both lvalues? or What would be an appropriate example? Edit: What about…
user1086635
  • 1,536
  • 2
  • 15
  • 21
3
votes
1 answer

Where is the rvalue coming from?

I learning about references and value categories because the latter are mentioned in some C++ errors. I have a function, referenceToDouble that takes in references to double. From watching this video on value categories, I believe that left and…
heretoinfinity
  • 1,528
  • 3
  • 14
  • 33
3
votes
1 answer

why gcc 6.4.0 c++14 moves automatically lvalue to rvalue

I encountered a problem where gcc compiler moved local variable (not temporary) as rvalue argument to a function. I have a simple example: class A { public: A() {} A& operator=(const A&) { std::cout << "const A&\n"; return *this; } A&…
3
votes
3 answers

Is the right operand of an assignment always converted to an rvalue?

I would like a clarification about this particular case: class Test { Test& operator=(const Test& copy) { ... } Test() = default; } Test a; Test b; b = a; //Is "a" converted to an rvalue? "a" is an lvalue, however it is now…
yggdrasil
  • 737
  • 5
  • 14
3
votes
1 answer

Does a Comparison Between an Lvalue and a Literal Invoke an Lvalue-to-Rvalue Conversion?

I asked this question: static_assert of const Variable And apparently it comes down to the question does a floating point lvalue get converted to an rvalue for the purposes of comparison? So in this code does an lvalue-to-rvalue conversion…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
3
votes
3 answers

lvalue-to-rvalue conversion of an array in ISO C

C++ ANSI ISO IEC 14882 2003 Annex C.1 (page 668): Change: The result of a conditional expression, an assignment expression, or a comma expression may bean lvalue Rationale: C + + is an object-oriented language, placing relatively more emphasis on…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
2
votes
1 answer

Intended invalid intialization from rvalue using *this

In C++ it is impossible to bind an r-value argument to a non-const l-value reference. But I noticed that when I call on the r-value object the method returning *this it compiles somehow. Example: Obliviously this snippet od code will not compile …
Patryk
  • 136
  • 8
2
votes
0 answers

Should I explicitly =delete non-const rvalue ref qualified function?

I have a class A that has a non-const member function foo that must not be called on temporaries, this is how it looks like: struct A { void foo() & {...} }; If foo were const I would have to explicitly delete the const&& version since…
user7769147
  • 1,559
  • 9
  • 15