A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/type_traits/integral_constant/ below:

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; }
};
1
2
3
4
5
6
7
8
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() const noexcept { return v; }
  constexpr T operator()() const noexcept { 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 type definition value_type The type of the constant (template parameter T) type The integral_constant type itself
Member functions
operator value_type
Returns value (public member function)

Instantiations
true_type
True type (class)
false_type
False type (class)

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:


See also
true_type
True type (class)
false_type
False type (class)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4