<type_traits>

class template
<type_traits>

std::add_const

template <class T> struct add_const;
Add const qualification
Obtains the type T with const qualification.

The transformed type is aliased as member type add_const::type.

If T is not already const-qualified, and is neither a reference nor a function (which cannot be const-qualified), this is the same type as T const. Otherwise, it is T unchanged.

Notice that this class merely obtains a type using another type as model, but it does not transform values or objects between those types. To explicitly add const-qualification to an object, const_cast can be used.

Template parameters

T
A type.

Member types

member typedefinition
typeIf T is not const-qualified, and is neither a reference nor a function, the same type as T but const-qualified.
Otherwise, T.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// add_const example
#include <iostream>
#include <type_traits>

int main() {
  typedef std::add_const<int>::type A;         // const int
  typedef std::add_const<const int>::type B;   // const int     (unchanged)
  typedef std::add_const<const int*>::type C;  // const int* const
  typedef std::add_const<int* const>::type D;  // int* const    (unchanged)
  typedef std::add_const<const int&>::type E;  // const int&    (unchanged)

  std::cout << std::boolalpha;
  std::cout << "checking constness:" << std::endl;
  std::cout << "A: " << std::is_const<A>::value << std::endl;
  std::cout << "B: " << std::is_const<B>::value << std::endl;
  std::cout << "C: " << std::is_const<C>::value << std::endl;
  std::cout << "D: " << std::is_const<D>::value << std::endl;
  std::cout << "E: " << std::is_const<E>::value << std::endl;

  return 0;
}

Output:
checking constness
A: true
B: true
C: true
D: true
E: false


See also