class template
<type_traits>
std::integral_constanttemplate <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; }
};
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;
}
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