std::add_const in C++ with Examples

The std::add_const template of C++ STL is present in the <type_traits> header file. The std::add_const template of C++ STL is used to get the type T with const qualification. The std::is_volatile is used to check if type T is const qualified or not.

Header File:

#include<type_traits>

Template Class:

template < class T >
struct add_const;

Syntax:

std::add_const<T>::value

Parameter: The template std::add_const accepts a single parameter T(Trait class) which is used to declare the type T as a const qualified.



Below is the program to demonstrate std::add_const: in C++:

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// std::add_const
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Driver Code
int main()
{
  
    // Declare variable of type
    // int, const int, const int*,
    // int * const and const int&
    typedef add_const<int>::type A;
    typedef add_const<const int>::type B;
    typedef add_const<const int*>::type C;
    typedef add_const<int* const>::type D;
    typedef add_const<const int&>::type E;
  
    cout << std::boolalpha;
  
    // Checking const of the above
    // declared variables
    cout << "A is const int? "
         << is_same<const int, A>::value
         << endl;
  
    cout << "B is const int? "
         << is_same<const int, B>::value
         << endl;
  
    cout << "C is const int? "
         << is_same<const int, C>::value
         << endl;
  
    cout << "D is const int? "
         << is_same<const int, D>::value
         << endl;
  
    std::cout << "E is const int? "
              << is_same<const int, E>::value
              << endl;
  
    return 0;
}

chevron_right


Output:

A is const int? true
B is const int? true
C is const int? false
D is const int? false
E is const int? false

Reference: http://www.cplusplus.com/reference/type_traits/add_const/

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.