Skip to content
Related Articles

Related Articles

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:




// C++ program to illustrate
// std::add_lvalue_reference
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Driver Code
int main()
{
  
    // Declare lvalue of type int, int&,
    // int&& and int*
    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;
  
    // Check if A is int const& or not
    cout << "A: " << is_same<int const&, A>::value
         << endl;
  
    // Check if B is int* or not
    cout << "B: " << is_same<int*, B>::value
         << endl;
  
    // Check if C is int& or not
    cout << "C: " << is_same<int&, C>::value
         << endl;
  
    // Check if C is int && or not
    cout << "D: " << is_same<int&&, D>::value
         << endl;
  
    return 0;
}
Output:
A: true
B: false
C: true
D: false

Program 2:




// C++ program to illustrate
// std::add_lvalue_reference
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Driver Code
int main()
{
  
    // Declare lvalue using typename
    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;
  
    // Check if the above declaration
    // are correct or not
    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/

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :