is_lvalue_reference Template in C++

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of std::is_lvalue_reference template in C++ STL.

is_lvalue_reference template in C++ is used to check whether the defined type is an lvalue reference or not.

What is an lvalue?

Lvalues are the values which are on the left side of an assignment operator. Lvalues are the expressions which refer to the memory location.

What is Lvalue reference?

Lvalue reference is a reference which binds to a lvalue. This is very similar to how we used to refer to a variable in traditional C++ or C language, i.e. by using ampersand symbol (&) with the variable itself to refer to its address.

Example

int& a;

Syntax

template <class T> is_lvalue_reference;

Parameters

The template can have only parameter of type T, and check whether the given type is a lvalue reference or not

Return value

It returns a Boolean value, true if the given value is lvalue reference, and false if the given value is not a lvalue reference or the when we are referencing to an unknown location

Example

Input: is_lvalue<int &>::value;
Output: True

Input: is_lvalue<int>::value;
Output: False

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
class TP {
};
int main() {
   cout << std::boolalpha;
   cout << "Checking for is_lvalue_reference: ";
   cout << "\nTP class : "<<is_lvalue_reference<TP>::value;
   cout << "\nTP& : "<< is_lvalue_reference<TP&>::value;
   cout << "\nTP&&: "<< is_lvalue_reference<TP&&>::value;
   return 0;
}

Output

If we run the above code it will generate the following output −

Checking for is_lvalue_reference:
TP class : false
TP& : true
TP&&: false

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
int main() {
   cout << std::boolalpha;
   cout << "Checking for is_lvalue_reference: ";
   cout << "\nint : "<<is_lvalue_reference<int>::value;
   cout << "\nint& : "<< is_lvalue_reference<int&>::value;
   cout << "\nint&&: "<< is_lvalue_reference<int&&>::value;
   cout << "\nchar : "<<is_lvalue_reference<char>::value;
   cout << "\nchar& : "<< is_lvalue_reference<char&>::value;
   cout << "\nchar&&: "<< is_lvalue_reference<char&&>::value;
   cout << "\nfloat : "<<is_lvalue_reference<float>::value;
   cout << "\nfloat& : "<< is_lvalue_reference<float&>::value;
   cout << "\nfloat&&: "<< is_lvalue_reference<float&&>::value;
   cout << "\ndouble : "<<is_lvalue_reference<double>::value;
   cout << "\ndouble& : "<< is_lvalue_reference<double&>::value;
   cout << "\ndouble&&: "<< is_lvalue_reference<double&&>::value;
   return 0;
}

Output

If we run the above code it will generate the following output −

Checking for is_lvalue_reference:
int : false
int& : true
int&&: false
char : false
char& : true
char&&: false
float : false
float& : true
float&&: false
double: false
double : true
double&&: false
raja
Published on 23-Mar-2020 07:12:08
Advertisements