std::add_lvalue_reference in C++ with Examples
Last Updated :
12 Jun, 2020
The std::add_lvalue_reference template of C++ STL is present in the <type_traits> header file. The std::add_lvalue_reference template of C++ STL is used to get lvalue reference type that refers to T type. An lvalue reference is formed by placing an ‘&’ after some type. An rvalue reference is formed by placing an ‘&&’ after some type. The lvalue cannot bind a (non const) lvalue reference to an rvalue.
The std::add_lvalue_reference is check by the template std::is_lvalue_reference::value.
Header File:
#include<type_traits>
Template Class:
template< class T >
struct add_lvalue_reference
Syntax:
std::is_lvalue_reference<T>::value
Parameter: The template std::add_lvalue_reference accepts a single parameter T(Trait class)
Below is the program to demonstrate std::add_lvalue_reference in C++:
Program 1:
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
int main()
{
typedef add_lvalue_reference< int const &>::type A;
typedef add_lvalue_reference< int &>::type B;
typedef add_lvalue_reference< int &&>::type C;
typedef add_lvalue_reference< int &>::type D;
cout << std::boolalpha;
cout << "A: " << is_same< int const &, A>::value
<< endl;
cout << "B: " << is_same< int *, B>::value
<< endl;
cout << "C: " << is_same< int &, C>::value
<< endl;
cout << "D: " << is_same< int &&, D>::value
<< endl;
return 0;
}
|
Output:
A: true
B: false
C: true
D: false
Program 2:
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
int main()
{
using a = int ;
using b = float ;
using lref
= typename add_lvalue_reference<a>::type;
using rref
= typename add_rvalue_reference<b>::type;
cout << std::boolalpha;
cout << "is int is lvalue? "
<< is_lvalue_reference<a>::value
<< '\n' ;
cout << "is lref is lvalue? "
<< is_lvalue_reference<lref>::value
<< '\n' ;
cout << "is float is lvalue? "
<< is_rvalue_reference<b>::value
<< '\n' ;
return 0;
}
|
Output:
is int is lvalue? false
is lref is lvalue? true
is float is lvalue? false
Reference: http://www.cplusplus.com/reference/type_traits/add_lvalue_reference/
Share your thoughts in the comments
Please Login to comment...