std::add_lvalue_reference in C++ with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

A: true
B: false
C: true
D: false

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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/

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.