4

I have been studying addition of rvalue reference in C++11. It wasn't straightforward but I feel like I am finally starting to get a grasp of it. However there is one particular instance where I am confused. Specifically I don't get what is the meaning of 'b' in this example:

int a = 27;
int&& b = 27;

EDIT: I know that int a = 27 is an lvalue not lvalue reference.

Also what I looking for is not what int&& b = 27 is but what is it meaning intuitively. I am confused because I thought that rvalues where not addressable, but here we have a reference to rvalue which means we can adress it. So how come it is still an rvalue?

dawid
  • 665
  • 2
  • 6
  • 12
  • possible dupplicate for https://stackoverflow.com/questions/34899538/auto-variables-are-not-rvalue-reference – Elvis Dukaj May 23 '18 at 09:45
  • @elvis.dukaj That's not the correct duplicate – babu646 May 23 '18 at 09:46
  • I know that int a= 27 is not an lvalue reference. It's an lvalue. I was just showing this as they seem to mean the same thing here ( although I know they don't). Also, I am sure there is million of questions about lvalue and rvalue but I specifically want to know what 'int&& = 27' mean. – dawid May 23 '18 at 09:48
  • Possible duplicate of [What are rvalues, lvalues, xvalues, glvalues, and prvalues?](https://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues) – Paul Floyd May 23 '18 at 09:52
  • Possible duplicate of [What happens when you assign a literal constant to an rvalue reference?](https://stackoverflow.com/questions/33085796/what-happens-when-you-assign-a-literal-constant-to-an-rvalue-reference) – xskxzr May 23 '18 at 12:45

3 Answers3

6

int a = 27; is a statement, and is neither an lvalue nor an rvalue. It defines the name a, which can be used as an lvalue expression, of type int.

int&& b = 27; is also a statement and the name b is used as an lvalue expression of type int&&, and permits a conversion to an xvalue expression of type int

Caleth
  • 52,200
  • 2
  • 44
  • 75
  • I see. So int&& can be explicitly converted to int? – dawid May 23 '18 at 10:04
  • @dawid You can initialise an `int` from an `int&&` just like from an `int` or an `int&`, yes. In the specific case of `int` there is no difference, but initialising a `std::vector` from a `std::vector&&` will do something different than from a `std::vector&` – Caleth May 23 '18 at 10:06
5

but here we have a reference to rvalue which means we can adress it. So how come it is still an rvalue?

Given int&& b = 27;, a temporary int is constructed from 27 and then b binds to the temporary. (And the lifetime of the temporary is extended to the lifetime of b.) After that if you get the address like &b you'll get the address of the temporary.

BTW b is an lvalue itself.

Temporary objects are created when a prvalue is materialized so that it can be used as a glvalue, which occurs (since C++17) in the following situations:

  • binding a reference to a prvalue

BTW: It's not special for rvalue reference; same thing happens for const int& b = 27;.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
-1

B is rvalue reference and you can assign only rvalue to it.

code707
  • 1,663
  • 1
  • 8
  • 20