is_rvalue_reference Template in C++

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of std::is_rvalue_reference template in C++ STL. is_rvalue_reference template in C++ is used to check whether the defined type is a rvalue reference or not.

What is an rvalue?

Rvalues are the values which are on the right side of an assignment operator. Rvalues are the value

What is rvalue reference?

Rvalue reference is identified by double ampersand symbol (&&). This is used to initialise it with only the rvalue value.

Syntax

int&& a;

Syntax

template <class T> is_rvalue_reference;

Parameters

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

Return value

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

Example

Input: is_rvalue<int&>::value;
Output: False

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

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_rvalue_reference<int>::value;
   cout << "\nint& : "<< is_rvalue_reference<int&>::value;
   cout << "\nint&&: "<< is_rvalue_reference<int&&>::value;
   cout << "\nchar : "<<is_rvalue_reference<char>::value;
   cout << "\nchar& : "<< is_rvalue_reference<char&>::value;
   cout << "\nchar&&: "<< is_rvalue_reference<char&&>::value;
   cout << "\nfloat : "<<is_rvalue_reference<float>::value;
   cout << "\nfloat& : "<< is_rvalue_reference<float&>::value;
   cout << "\nfloat&&: "<< is_rvalue_reference<float&&>::value;
   cout << "\ndouble : "<<is_rvalue_reference<double>::value;
   cout << "\ndouble& : "<< is_rvalue_reference<double&>::value;
   cout << "\ndouble&&: "<< is_rvalue_reference<double&&>::value;
   return 0;
}

Output

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

Checking for is_rvalue_reference:
int : false
int& : false
int&&: ture
char : false
char& : false
char&&: ture
float : false
float& : false
float&&: ture
double: false
double : false
double&&: ture

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_rvalue_reference<TP>::value;
   cout << "\nTP& : "<< is_rvalue_reference<TP&>::value;
   cout << "\nTP&&: "<< is_rvalue_reference<TP&&>::value;
   return 0;
}

Output

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

Checking for is_rvalue_reference:
TP class : false
TP& : false
TP&&: true
raja
Published on 23-Mar-2020 07:15:23
Advertisements