lvalues references and rvalues references in C++ with Examples - GeeksforGeeks

02 Sep, 2021
Prerequisites: lvalue and rvalue in C++, References in C++“l-value” refers to a memory location that identifies an object. “r-value” refers to the data value that is stored at some address in memory. References in C++ are nothing but the alternative to the already existing variable. They are declared using the ‘&’ before the name of the variable.
Example: 
int a = 10;

// Declaring lvalue reference
int& lref = a;

// Declaring rvalue reference
int&& rref = 20;
Below is the implementation for lvalue and rvalue:
C++
// C++ program to illustrate the// lvalue and rvalue #include <iostream>using namespace std; // Driver Codeint main(){    // Declaring the variable    int a{ 10 };     // Declaring reference to    // already created variable    int& b = a;     // Provision made to display    // the boolean output in the    // form of True and False    // instead of 1 and    cout << boolalpha;     // Comparing the address of both the    // variable and its reference and it    // will turn out to be same    cout << (&a == &b) << endl;    return 0;}
true
 
Explanation: The following code will print True as both the variable are pointing to the same memory location. b is just an alternative name to the memory assigned to the variable a. The reference declared in the above code is lvalue reference (i.e., referring to variable in the lvalue) similarly the references for the values can also be declared.
rvalue references have two properties that are useful: 
rvalue references extend the lifespan of the temporary object to which they are assigned.Non-const rvalue references allow you to modify the rvalue.
rvalue references extend the lifespan of the temporary object to which they are assigned.
Non-const rvalue references allow you to modify the rvalue.
Important: lvalue references can be assigned with the rvalues but rvalue references cannot be assigned to the lvalue. 
C++
// C++ program to illustrate the// lvalue and rvalue#include <iostream>using namespace std; // Driver Codeint 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;     // Value of both a    // and lref is changed    lref = 30;     // Value of rref is changed    rref = 40;    cout << "lref = " << lref << endl;    cout << "rref = " << rref << endl;     // This line will generate an error    // as l-value cannot be assigned    // to the r-value references    // int &&ref = a;    return 0;}
lref = 10
rref = 20
lref = 30
rref = 40
 
Uses of the lvalue references: 
lvalue references can be used to alias an existing object.They can also be used to implement pass-by-reference semantics.
lvalue references can be used to alias an existing object.
They can also be used to implement pass-by-reference semantics.
C++
// C++ program to illustrate lvalue#include <iostream>using namespace std; // Creating the references of the// parameter passed to the functionvoid swap(int& x, int& y){    int temp = x;    x = y;    y = temp;} // Driver Codeint main(){    // Given values    int a{ 10 }, b{ 20 };    cout << "a = " << a         << " b = " << b << endl;     // Call by Reference    swap(a, b);     // Print the value    cout << "a = " << a         << " b = " << b << endl;    return 0;}
a = 10 b = 20
a = 20 b = 10
 
Note: When the function return lvalue reference the expression becomes lvalue expression.
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 lvalue of type ‘int’.
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 lvalue of type ‘int’.
Program 1:  
C++
// C++ program to illustrate rvalue#include <iostream>using namespace std; // lvalue reference to the lvalue// passed as the parametervoid printReferenceValue(int& x){    cout << x << endl;} // Driver Codeint main(){    // Given value    int a{ 10 };     // Function call is made lvalue & can    // be assigned to lvalue reference    printReferenceValue(a);    return 0;}
10
 
Program 2: 
C++
// C++ program to illustrate rvalue#include <iostream>using namespace std; // Declaring rvalue reference to the// rvalue passed as the parametervoid printReferenceValue(int&& x){    cout << x << endl;} // Driver Codeint main(){    // Given value a    int a{ 10 };     // Works fine as the function is    // called with rvalue    printReferenceValue(100);    return 0;}
100
 
adnanirshad158
surindertarika1234
simranarora5sos
gulshankumarar231
C++-References
cpp-references
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
Inline Functions in C++
Pair in C++ Standard Template Library (STL)
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++
Destructors in C++