Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Examples
3.
Creating References in C++
3.1.
Example 1
3.2.
Example 2
3.2.1.
Explanation
4.
Important Properties of Rvalue References
4.1.
Example to Illustrate Lvalue and Rvalue
5.
Uses of Lvalue References
5.1.
Example
6.
Uses of Rvalue References
6.1.
Example 1
6.2.
Example 2
7.
Frequently Asked Questions
7.1.
What is Rvalue in C++?
7.2.
What is Lvalue in C++?
7.3.
What do you mean by references in C++?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

Lvalue and Rvalue References in C++

Author Aditya kumar
0 upvote

Introduction

First of all, let’s be faraway from the formal definitions of lvalue and rvalue. In C++ an lvalue is some things that point to a selected memory location. On the opposite hand, an rvalue is some things that do not point anywhere.

lvalue and rvalue

Generally, Rvalues are temporary, while lvalues live an extended life since they exist as variables. It is also fun to consider lvalues as containers and rvalues as things contained within the containers. Without a container, they might expire.

Let me show you some examples.

int x = 666; // ok

Here 666 is an rvalue; variety (technically a literal constant) has no specific memory address, apart from some temporary register while the program is running. That number is assigned to x, which may be a variable. A variable features a specific memory location, so its an lvalue. C++ states that an assignment requires an lvalue as its left operand: this is often perfectly legal.

Then with x, which is an lvalue, you’ll do stuff like that:

int* y = &x; // ok

Here I’m taking the address of x and assigning it to y, through the address-of operator &. It takes an lvalue argument value and outputs an rvalue.

The Lvalue refers to a modifiable object in c++ that can be either left or right side of the assignment operator.
The Rvalue refers to a value stored at an address in the memory. It can appear only on the right-hand side of the assignment operator.

Also see, Literals in C.Fibonacci Series in C++

Examples

/* Example – 1 */
int var; // var is an object of type int
var = 1;
// Here var is lvalue and Rvalue is ‘1’
1=var;
// Invalid expression as 1 is lvalue that can’t be in the left

 

/* Example 2 */
int z;
z=1;
int m;
m=z; // Valid

A reference is another name to an existing variable. It uses the ‘&’ sign before the variable name. References vs Pointers: References and pointers are almost times confused. Here are three points to keep in mind regarding these:

  • You cannot have NULL references, you want to always be ready to assume that a reference is connected to a legitimate piece of storage.
  • Once a reference is initialised to an object, it can’t be changed to ask another object. Pointers are often pointed to a different object at any time.
  • A reference must be initialised when it’s created. Pointers are often initialised at any time.

Creating References in C++

Assume a variable name as a label attached to its location in memory. It is same as you have a registered name in college and nickname at home. Therefore, you can access the value using its original name or its reference name.

Example

int i = 17;

Creating a reference variable for I as follows.

int& r = i;

Read the & in these declarations as reference. Thus, “r” is another name reference initialized to “i” and read the second declaration as “s is a double reference initialised to “d”.

Example 1

include<iostream> using namespace std;

int main () {
	// declare simple variables
	int i;
	double d;
	// declare reference variables
	int& r = i;
	double& s = d;
	i = 5;
	cout << “Value of i : ” << i << endl;
	cout << “Value of i reference : ” << r << endl;
	d = 11.7;
	cout << “Value of d : ” << d << endl;
	cout << “Value of d reference : ” << s << endl;
	return 0;
}

When the above code is compiled together and executed, it produces the following result −

Value of i: 5
Value of i reference: 5
Value of d: 11.7
Value of d reference: 11.7

Example 2

int s = 10;
// Declaring lvalue reference
int& l_ref = s;
// Declaring rvalue reference
int&& r_ref = 20;
–> Example for lvalue and rvalue reference
// C++ program to illustrate the
// lvalue and rvalue
include<iostream>
using namespace std;
int main()
{
	// Declaring the variable
	int a{ 10 };
	// Declaring reference to
	// already created variable
	int& b = a;
	// On comparing the variable and address
	// the result will come same
	// as both are pointing to same location
	cout << (&a == &b) << endl;
	return 0;
}

Explanation

The code outputs ‘true’ as b is an lvalue reference to a.

Both of them are pointing to the same memory locations. b is an alternative name to the memory assigned to a.

You can also read about the memory hierarchy.

Important Properties of Rvalue References

  • Rvalue references extend the life of the object to which they are referred(assigned).
  • Non-const rvalue allows for changing the value.

Note: lvalue references can be assigned to Rvalue references but not vice versa.

Example to Illustrate Lvalue and Rvalue

include<iostream>
using namespace std;
int main()
{
	int a = 10;
	// Declaring lvalue reference
	// (i.e variable a)
	int& lref = a;
	// Declaring rvalue reference
	int&& rref = 20;
	// Print the values
	cout << “lref = ” << lref << endl;
	cout << “rref = ” << rref << endl;
	//changing the value of lref
	lref = 30;
	// Changing the value of rref
	rref = 40;
	cout << “lref = ” << lref << endl;
	cout << “rref = ” << rref << endl;
	// Error – The lvalue references cant be assigned to rvalue references
	// int &&ref = a;
	return 0;
}

Output

lref = 10
rref = 20
lref = 30
rref = 40

Uses of Lvalue References

  • Lvalues can be used as an alias to an existing object.
  • They can also be used in implementing pass by reference semantics.

Example

include<iostream>
using namespace std;
// references of the parameter passed to the function swap
void swap(int& x, int& y)
{
	int temp = x;
	x = y;
	y = temp;
}
int main()
{
	// initial values
	int a{ 10 }, b{ 20 };
	cout << “a = ” << a << ” b = ” << b << endl; // Function call –>Call by Reference
	swap(a, b);
	// Print the swapped values
	cout << “a = ” << a << ” b = ” << b << endl;
	return 0;
}

Output

a = 10 b = 20
a = 20 b = 10

Uses of Rvalue References

  • They are used in working with the move constructor and move assignment.
  • Cannot bind non-const lvalue reference of type ‘int&‘ to an rvalue of type ‘int’.
  • Cannot bind rvalue references of type ‘int&&‘ to an lvalue of type ‘int’.

Example 1

include<iostream>
using namespace std;
// lvalue reference to the lvalue
// passed as the parameter
void printReferenceValue(int& x)
{
	cout << x << endl;
}
int main()
{
	// initial value
	int a{ 10 };
	// Function call is made lvalue & can
	// be assigned to lvalue reference
	printReferenceValue(a);
	return 0;
}

Output

10

Example 2

include<iostream>
using namespace std;
// Declaring rvalue reference to the
// rvalue passed as the parameter
void printReferenceValue(int&& x)
{
	cout << x << endl;
}
// Driver Code
int main()
{
	// Given value a
	int a{ 10 };
	// Works fine as the function is
	// called with rvalue
	printReferenceValue(100);
	return 0;
}

Output

100

 

Also check out this article - Pair in C++

Frequently Asked Questions

What is Rvalue in C++?

In C++, an Rvalue is a temporary object or a value that is not associated with an object. An Rvalue can be thought of as a value that is used to initialize an object, or as a value that is returned from a function, but is not stored in a variable.

What is Lvalue in C++?

Lvalue refers to an object that persists in memory and has a name. An Lvalue can be thought of as an object that can be assigned a value, whereas an rvalue is a temporary object that cannot be assigned a value.

What do you mean by references in C++?

In C++, a reference is a way to refer to an existing object, using another name. A reference is similar to a pointer, but with some important differences. When you create a reference, you are creating an alias for an existing object, rather than creating a new object.

Conclusion

In this article, we have discussed Lvalue and Rvalue references in C++. We have discussed how we can create the references in C++. We have also discussed its implementation. 

Also, refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScript, and many more! If you wish to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! 

But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Recommended Readings:

Live masterclass