Skip to content
Related Articles

Related Articles

std::add_const in C++ with Examples
  • Last Updated : 12 Jun, 2020

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 C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :