Very confused

In my book Book, Ivor Horton's Beginning Visual C++ 2012 it says that the following is a lvalue reference?


const int& refData = 5;

Isnt it a rvalue reference....

Im very confused because isnt 5 a rvalue because it cannot be on the left side of the = sign because it does not have a memory location defined.

Can anyone shed some light on this?

Please and thankyou :D
The constant '5' is not an lvalue, but refData is.
No, it's an lvalue reference like the book says.

rvalue references are denoted by double &&'s, such as this:

1
2
int foo;
int&& refData = foo;  // <- rvalue reference 



Im very confused because isnt 5 a rvalue because it cannot be on the left side of the = sign because it does not have a memory location defined.


Yeah the naming is confusing. It's an lvalue reference... but it's const, which is why it can't appear on the left of the = operator.

Likewise, rvalue references can appear on the left side of the = operator.

Personally I think the naming convention is absurd and confusing. So don't worry if you find yourself still being confused. I'm even still confused why they're named the way they are. I also had to look this up to make sure I was telling you the right thing.


If you're interested in more reading, this page is very informative:
http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html
@Disch

Im still confused but you shed a bit of light. Thank you very much. I really appreciate it.
isnt 5 a rvalue because it cannot be on the left side of the = sign because it does not have a memory location defined.

The = sign is inconsequential, but you're right to pick on memory location: lvalue expressions identify objects that have memory locations, rvalue expressions identify objects that don't.

What you need to keep in mind, if you really want to understand this, is that lvalue and rvalue are categories of expressions, while lvalue reference and rvalue reference are types of variables.

5 is an expression whose value category is rvalue (since any non-string literal is rvalue).

refData is a variable whose type is lvalue reference (to const int). This has nothing to do with what 5 is. It's just the type you used in declaration.

Isnt it a rvalue reference....

An rvalue reference, as Disch correctly points out, is a variable with rvalue reference type, such as int&&.

Here's another helpful (hopefully) link: http://en.cppreference.com/w/cpp/language/value_category
Thank you both very much.
@cubbi
@disch
But what is the difference between rvalue references and lvalue references?

And one more question. Arent lvalue references a alias for only a object but 5 isnt a object.

Im just confused on that.
Last edited on
is this a good definition for the difference?

An rvalue reference behaves just like an lvalue reference except that it can bind to a temporary (an rvalue), whereas you can not bind a (non const) lvalue reference to an rvalue.
references is an alias to memory location which contains a value of given type. In your case compiler will allocate memory where "5" will be stored.

And in your example refData is lvalue reference, but rvalue expression.
what is the difference between rvalue references and lvalue references?

The main difference is when a function is overloaded such that one overload expects lvalue reference paramter, and another overload expects rvalue reference parameter, then when the function is called with lvalue argument, the first overload is selected, and when the function is called with rvalue argument, the second overload is selected.

Arent lvalue references a alias for only a object but 5 isnt a object.
5 isn't an object, but const int& refData = 5; creates an object of type int. You can take its address later if you'd like.

See http://en.cppreference.com/w/cpp/language/reference_initialization for some more details on reference initialization. This case falls under "Otherwise, a temporary of type T is constructed and copy-initialized from object."
@cubbi
@miinipaa
@disch

is this a good defination for the difference?

is this a good definition for the difference?

An rvalue reference behaves just like an lvalue reference except that it can bind to a temporary (an rvalue), whereas you can not bind a (non const) lvalue reference to an rvalue.
Topic archived. No new replies allowed.