Open In App

std::add_const in C++ with Examples

Last Updated : 12 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


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/



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads