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
1
vote
1 answer

Is it an Rvalue or Lvalue After a Cast

The code here test for lvalue or rvalue after a type cast: #include template T const f1(T const &t) { printf("T const \n"); return t; } template T f1(T &t) { printf("T\n"); return t; } struct KK { …
JavaMan
  • 4,954
  • 4
  • 41
  • 69
0
votes
5 answers

Why do we need the address-of operator (ampersand, &) when passing a variable to scanf, but not when assigning to it?

When using the scanf function, we cannot simply specify the variable name. Rather, we have to prefix the variable name with an ampersand, e.g. int a; printf("enter your value"); scanf("%d", &a);. I checked online sources searching for the reason.…
kishoreraj
  • 25
  • 4
0
votes
1 answer

non-trivial rvalue and lvalue example

I am not sure about some rvalue/lvalue non-trvial examples. are std::vector({1,2,3})[0] and std::vector() expressions below lvalue or rvalue? the code has no actual usage but it surprises me a bit that the code is valid. It looks like they…
rnd_nr_gen
  • 2,203
  • 3
  • 36
  • 55
0
votes
1 answer

Lvalue-to-rvalue conversion for class types: is there copying involved?

(I asked this question before but didn't give a viable example so I deleted previous one. I hope on this one I got the example right.) Case: #include struct S { S() = default; S(const S &) { std::cout << "Copying" <<…
0
votes
1 answer

Lvalue-to-rvalue conversion in [expr.ref]/2

[expr.ref]/2: For the first option (dot) the first expression shall be a glvalue having complete class type. For the second option (arrow) the first expression shall be a prvalue having pointer to complete class type. The expression E1->E2 is…
Alexander
  • 2,581
  • 11
  • 17
0
votes
2 answers

Difference between std::forward implementation

Recently I've been trying to understand move semantics and came up with a question. The question has already been discussed here. I implemented the first variant and checked whether it returns l-value or r-value: #include using…
0
votes
1 answer

Delete a node from binary search tree in C

I am trying to delete a node from a binary search tree. But when I want to test the function, I'll get an error message in 'case 3: two children'. clist->name = temp->name; This line causes the error message. It says: the left operand has to be an…
stefOCDP
  • 803
  • 2
  • 12
  • 20
0
votes
0 answers

How std::move can work with copy-constructor that takes non-const reference?

I was reading about std::move. Based on quite few materials, I concluded that std::move is just a function that converts its argument type to the rvalue-reference. I also read that, rvalue-references can be used with the functions that take their…
Validus Oculus
  • 2,756
  • 1
  • 25
  • 34
0
votes
2 answers

How does rvalue reference work here?

I am puzzled by the following code: #include int main() { int x{}; int&& rvx = static_cast(x); ++rvx; std::cout << x << std::endl; } The output of it is 1. I don't understand how this works. The static_cast is…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
0
votes
2 answers

L-value to R-value conversion

the site here says that: http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc05lvalue.htm If an lvalue appears in a situation in which the compiler expects an rvalue, the compiler…
Duggs
  • 169
  • 1
  • 12
0
votes
2 answers

When Exactly L-Value to R-Value Conversion Happens In C

If an lvalue appears in a situation in which the compiler expects an rvalue, the compiler converts the lvalue to an rvalue. An lvalue e of a type T can be converted to an rvalue if T is not a function or array type. The type of e after conversion…
Duggs
  • 169
  • 1
  • 12
-1
votes
1 answer

How to disallow increment operator when operated multiple times on a object?

How do I overload the increment operator so that this code becomes invalid - Point p(2, 3); ++++p; while allowing the following - Point p(2, 3), a(0, 0), b(1, 4); a = ++p + b; Like in C this would be invalid - int a = 2; ++++a;
-1
votes
2 answers

error: invalid initialization of non-const reference of type 'std::function&' from an rvalue of type 'main()::'|

EDIT: Sorry, I asked this question without a thro understanding of references... I seem to be getting this error when I run this code... error: invalid initialization of non-const reference of type 'std::function&' from an rvalue of type…
Samuel
  • 315
  • 3
  • 14
-1
votes
1 answer

Why must a non-const reference be initialized with an lvalue?

Here is a snippet of code which causes a C2664 error: cannot convert argument 1 from 'std::unique_ptr>' to 'ComPtr &' So why must a non-const reference be initialized with an lvalue? How to avoid this except by…
Y.Lex
  • 241
  • 1
  • 7
-2
votes
1 answer

How to use forwarding to cast rvalue to lvalue reference?

I'm trying to use an overloaded function to hide passing an int reference to a utility function when I don't care for the returned int data. Why is it that I need to use T& instead of T as the template argument? And why do I need the intermediate…
kaoao
  • 11
  • 4
1 2 3
4