class template
<type_traits>

std::integral_constant

template <class T, T v>
struct integral_constant;
Integral constant
This template is designed to provide compile-time constants as types.

It is used by several parts of the standard library as the base class for trait types, especially in their bool variant: see true_type and false_type.

Its definition in the Standard Library has the same behavior as:
1
2
3
4
5
6
7
template <class T, T v>
struct integral_constant {
  static constexpr T value = v;
  typedef T value_type;
  typedef integral_constant<T,v> type;
  constexpr operator T() { return v; }
};


Template parameters

T
Type of the integral constant.
Aliased as member type integral_constant::value_type.
v
Value of the integral constant.
Accessible as member integral_constant::value, or through type casting.

Member types

member typedefinition
value_typeThe type of the constant (template parameter T)
typeThe integral_constant type itself

Member functions


Instantiations


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// factorial as an integral_constant
#include <iostream>
#include <type_traits>

template <unsigned n>
struct factorial : std::integral_constant<int,n * factorial<n-1>::value> {};

template <>
struct factorial<0> : std::integral_constant<int,1> {};

int main() {
  std::cout << factorial<5>::value;  // constexpr (no calculations on runtime)
  return 0;
}


Output:
120

See also

<type_traits>