integral_constant定义于头文件<type_traits>
可以参考的实现:
template<typename _Tp, _Tp __v>
struct integral_constant
{
static constexpr _Tp value = __v;
typedef _Tp value_type;
typedef integral_constant<_Tp, __v> type;
constexpr operator value_type() const
{
return value;
}
#if __cplusplus > 201103L
#define __cpp_lib_integral_constant_callable 201304
constexpr value_type operator()() const
{
return value;
}
#endif
};
template<typename _Tp, _Tp __v>
constexpr _Tp integral_constant<_Tp, __v>::value;
使用的方法:
#include <type_traits>
#include <iostream>
using namespace std;
int main()
{
typedef integral_constant<int, 2> two_t;
typedef integral_constant<int, 4> four_t;
cout<<"two_t and four_t are equal:"&l