template <bool Cond, typename Body>
struct WhileLoop;
template <typename Body>
struct WhileLoop<true, Body>
{
typedef typename WhileLoop<Body::Cond, typename Body::NextType>::type type;
};
template <typename Body>
struct WhileLoop<false, Body>
{
typedef typename Body::ResType type;
};
template <typename Body>
struct While
{
typedef typename WhileLoop<Body::Cond, Body>::type type;
};
template <typename T, T val>
struct integer
{
typedef T ValueType;
static const T Value = val;
typedef integer type;
};
template <int lhs, int rhs>
struct GCD
{
static const bool Cond = lhs % rhs != 0;
static const int result = lhs;
typedef integer<int, result> ResType;
typedef GCD<rhs, lhs % rhs> NextType;
};
int main()
{
cout << While<GCD<100, 56>>::type::Value << endl;
return 0;
}
刚学习模板元编程,实现了一个简单算法,分享给大家,欢迎大家批评指正~,有好的建议一定要反馈哦~