Sign in with Google to keep private notes

Understanding Lvals and Rvals

L-value is a name used to refer to an object. R-values are anything that are not L-values.

R-values are usually stored temporarily in a CPU register.
(Tiny exception: when the item is too big, then C++ will allocate memory for it)

L-values have addresses.

R-values don't.
(Not so tiny exception: in the case of classes. E.g. Data().y needs to have an address)

ldr r1, s1a        ; load the address of s1 into r1
ldr r2, [r1, #4]   ; load s1.y into r2
str r2, ia         ; store r2 into i

Reference types exist because we want our changes within a function to persist beyond the function without needing to ask the client to & their variable.

And you might want to pass by const reference (const &) because you would want the performance win of passing by reference without the ability for the function to modify the underlying value.

Temporary Materialisation Storage

Now, the following usually doesn't work:

int *pi = &3;
int &ri = 3;

int i;
double *pd = &i;
double &rd = i;

The exception is when we use a const & if there's a valid conversion from the binding to the binded. This is done by allocating a temporary space for that conversion and that value is binded to the const reference (In the next article you will see const reference can bind to anything).

This also means you can pass any int-castable entity into the function.

(You'll later see that const l val refs can bind to all the underlying values. I think this is kind of the logic behind why that design decision was made.)

And it turns out: that these too are r values that occupy locations in memory.

And it turns out: a prvalue and an xvalue is dervied from this taxonomy.

It turns out: xvalues are rvalues with data storage, and pr values are rvalues that are truly temporary.

What we just described above is TMC (temporary materialisation conversion) that converts a prvlaue into an xvalue (probably by allocating some storage for it)

string operator+(string const &lo, string const &ro) will return an r-value. But as its an r value it has no address. As a result, you can't use string *p = &(s + t);.

But, if you define a move operator for temporary values

string::operator=(string&&) {}; 

then you can write string s1 = s + t; as a matter of fact s1 = s2 will still be a copy operation. If i want to trigger a move operation, I need to cast s2 into an xvalue with std::move(s2)

Q: If its an x value i cant get its address? I need to store that somewhere dont i?

A: No, you cannot directly take the address of an xvalue with the built-in & operator, even though it occupies memory (an xvalue is an rvalue, and & requires an lvalue).

Q: Anyway, once I've had it binded to a const ref, now does it have an address?

A: Once you bind it to a const& (or an &&), the reference variable itself is an lvalue. Because that reference is an lvalue, you can now take its address directly using &ref, which gives you the memory address of the underlying object. Binding to a reference extends the lifetime of a temporary to match the lifetime of the reference, ensuring the object and its address remain valid for that scope.

r values are usually parameters or return types, not really instantiated on the stack or passed around.

TMC will also trigger when r val ref binds to r val.

The reason why r val references passed into functions become l vals is because they can now live as long as the whole function. So in the context of that function yes its an l value. And you will try to solve that using perfect forwarding.

Finally, rval references can only bind to vals. rval references can never bind to lvalues. We go into this more in our investigation of value categories.

Articles to read next