0

I read a lot of rvalue lvalue related questions and blogs, and now I have the following question.

When assigning some object to reference variable, why is it OK that the right-hand-side is not rvalue.

int x = 11;
int& rx = x ; // x is lvalue.

I came to the following two possible answers for this question. Which is correct? Or what other answer is correct?

  1. x (lvalue) is implicitly converted to rvalue.
  2. int& rx = is not a usual assignment, and the right hand side need not be rvalue.

(ref.) * http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c * Does initialization entail lvalue-to-rvalue conversion? Is `int x = x;` UB?

Community
  • 1
  • 1
Toshihiro
  • 61
  • 1
  • 1
  • 7
  • Did you mean to write `int&& rx = x;`? That would be an rvalue reference. But those also bind to lvalues. – j6t Nov 05 '16 at 09:19
  • It seems you are making things more complicated than they need to be. Let's turn the question around: Why do you think that binding `rx` to `x` should *not* be OK? – Christian Hackl Nov 05 '16 at 09:21
  • int& rx = x is definitely what I want to ask. I got this question after I learned "implicit lvalue to rvalue conversion" and "lvalue is left-hand-side, and rvalue is right-hand-side". Before I learned that, I felt assigning lvalue reference to lvalue is natural. However, now I feel it may not be natural..... – Toshihiro Nov 05 '16 at 09:24
  • 5 is a rvalue. Do you think `int& rx = 5;` is OK? Why or why not? – n. m. could be an AI Nov 05 '16 at 09:28
  • @n.m. No. It's not ok, though I cannot explain it logically. So this equal assignment (in int& rx = 5) is not a usual assignment.. ?? – Toshihiro Nov 05 '16 at 09:30
  • @bluefog Thanks. So you think my second answer (in this question) is correct (or close to correct)? – Toshihiro Nov 05 '16 at 09:34
  • 1
    There is no assignment involvced. `int& rx = x` is an initialisation. You cannot assign to a reference. The right hand side **must** be a lvalue, it **can not be** a rvalue. – n. m. could be an AI Nov 05 '16 at 09:36
  • @n.m. thanks. Ok. Maybe I need to understand more about what assignment and initialization are... – Toshihiro Nov 05 '16 at 09:41
  • 1st mistake: `x` is an lvalue (it has a name). The l and r in lvalue and rvalue do not mean left and right any more. You have more reading in-front of you. Try this: http://en.cppreference.com/w/cpp/language/value_category it's a bit (very) dense - I am sure there are other explanations available. – Richard Critten Nov 05 '16 at 09:57
  • I don't agree that lvalue need always to be the left side of assignment operator.in fact I will prefer saying rvalue is that can't be on the left side, the rest will the lvalue – Eric Zheng Nov 05 '16 at 10:08
  • @Eric Zheng Thanks. I did some more research. As you say, lvalue can be in the right-hand-side. And reference assignment does not require rvalue on the right hand side. Lvalue can also be there. So rvalue-lvalue conversion is not used here. This is my conclusion. Thanks. – Toshihiro Nov 05 '16 at 11:37

0 Answers0