Search code examples
c++rvaluelvalue

Are R & L-values relative to its context?


so assume we write the following in C++: a=b=5; which basically means a=(b=5);

If we have a=5; I know that 5 is a literal and thus it is an R-Value. a is an L-Value. Same goes for b=5;

I'm wondering now, what happens, if we write a=b=5; respectively a=(b=5);

Can I now say the following?

For a, b=5 is an R-Value with a being an L-Value. Also, b is an L-Value and 5 is an R-Value.

What's the R-Value of a?


Solution

  • This depends. The built in operator= returns an lvalue reference to the left hand side of the assignment. So, if a and b are int's then (b = 5) is an lvalue expression and you assign that lvalue to a, with a and b both being lvalues and 5 being a prvalue.

    This is generally the same for overloaded operator= as well since most people return a lvalue reference but it does not have to be.

    If you want to cast a lvalue into a rvalue then you use std::move.