integral_constant

// from c++11 standard
namespace std {
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 value_type() { return value; }
};
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
}

————————————————
版权声明:本文为CSDN博主「csfreebird」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/csfreebird/article/details/44904121

#include <iostream>
#include <type_traits>

using namespace std;

//实现求阶乘
template<unsigned n>
struct fac : std::integral_constant<int, n*fac<n - 1>::value>{};
template<>
struct fac<0> : std::integral_constant<int, 1>{};


int main()
{
cout << fac<5>::value << endl;
return 0;
}

猜你喜欢

转载自www.cnblogs.com/xpylovely/p/12205375.html