In this article we will discuss the differences between lvalue and rvalue in C++.

In C, definition if lvalue and rvalue was somewhat simple, anything i.e. left of assignment operator is lvalue and  right ofassignment operator is rvalue. But in C++ this definition has changed and become more interesting. Every Expression in C++ is either rvalue or lvalue. Let’s discuss them one by one first.

What is lvalue ?

lvalue is anything whose address is accessible. It means we can take address of lvalue using & operator.  

Lets see some examples,

Example 1:

In above expression, x is an lvalue because we can access the address of x i.e.

Example 2:

In above expression, we can take address of z i.e.

Therefore, z is lvalue. But we cannot take the  address of  x+1 i.e.

Above line will not compile because we are trying to address of (x+1) and (x+1) doesn’t persist after single expression. As, we cannot take the address of (x+1). So, (x+1) is not an lvalue. So, what the heck is this (x+1) ?

As, lvalue is something whose address is accessible using & operator. But in this case, it’s not possible to access the address of (x+2). So, (x+2) is something that is not lvalue and that is exactly what rvalue is. (x+2) here is rvalue

What is rvalue ?

Rvalue is anything that is not lvalue. It means we cannot take address of rvalue and it also don’t persist beyond the single expression.  

Checkout some examples,

Example 3:

Here you can not take the address of 1 i.e.

Therefore, 1 is not a lvalue and hence it 1 is rvalue.

Example 4:

We cannot take the  address of  x+1 i.e.

Hence (x+1) is not a lvalue and hence (x+1) is rvalue.

Lets look into some expression and find out what is lvalue and what is rvalue,


Now, lets create a function that returns a integer i.e

Here, the value retutned by getData() is temporary i.e. it will not persist beyond the expression where it is called. Therefore it will get copied. Therefore, we can not take the address of temporary value returned by getData() i.e.

Therefore, getData() is a rvalue and we cannot take the address of rvalue.