64,654
社区成员
发帖
与我相关
我的任务
分享
// Factorial G
struct G {
private:
template <typename f, int n>
struct Meta {
const static unsigned __int64 Value = n * f::Apply<n - 1>::Value;
};
template <typename f>
struct Meta<f,1> {
const static unsigned __int64 Value = 1 ;
};
public:
template<typename _f>
struct Apply {
struct Type {
template <int _n>
struct Apply {
typedef Meta<_f,_n> Type;
const static unsigned __int64 Value = Type::Value;
};
};
};
};
// Fibonacci G
struct G2 {
private:
template <typename self,int n>
struct Meta {
const static unsigned __int64 Value = self::Apply<n -1>::Value + self::Apply<n -2>::Value;
};
template <typename self>
struct Meta<self,1> {
const static unsigned __int64 Value = 1;
};
template <typename self>
struct Meta<self,0> {
const static unsigned __int64 Value = 0;
};
public:
template <typename _self>
struct Apply {
struct Type {
template <int _n>
struct Apply {
typedef Meta<_self,_n> Type;
const static unsigned __int64 Value = Type::Value;
};
};
};
};
template< typename T1,typename T2,typename T3,typename T4 /* ... */ >
struct TMFunction {
};
template TMFucntion1 = TMFunction <Type1>;
auto a = sizeof (TMFucntion1<Type2,Type3,Type4>);
template TMFucntion2 = TMFunction <Type1,Type2>;
auto b = sizeof (TMFucntion2<Type3,Type4>);
template TMFucntion3 = TMFunction <Type1><Type2><Type3>;
auto c = sizeof (TMFucntion3<Type4>);
auto d = sizeof (TMFunction <Type1> <Type2> <Type3> <Type4>);
template<typename G>
struct Y {
struct W {
template <typename F>
struct Apply {
typedef typename F::Apply<F>::Type Type;
};
};
struct F {
template<typename f>
struct Apply {
// 下面这两行代码(#1,#2)不能通过编译,因为这时 F::Apply<>::Type 还没有定义,
// 所以不能递归地用于G::Apply<>::Type 的定义
/*
typedef typename W::Apply<f>::Type t; //#1
typedef typename G::Apply<t>::Type Type; //#2
*/
// 只能以下面的办法来解决
// 这里 “Type”的定义 并不依赖于 "G::Apply<>::Type",所以 "G::Apply<>::Type" 使用它不会构成递归定义
// 但是这使得Y失去了通用性,因为"Type"必须再重复"G::Apply<>::Type" 的定义
/**/
struct Type {
template< int n>
struct Apply {
typedef typename W::Apply<f>::Type t; //temp t
typedef typename G::Apply<t>::Type::Apply<n>::Type Type;
const static unsigned __int64 Value = Type::Value;
};
};
/**/
};
};
// the result is here
typedef typename W::Apply<F>::Type Type;
};
struct G3 {
template<typename f,typename T1,typename T2, int n>
struct Meta {
enum {Value = n + f::Apply<T1,T2, n - 1>::Value };
};
template<typename f,typename T1,typename T2>
struct Meta<0> {
enum {Value = 0 };
};
template<_f>
struct Apply {
struct Type {
//#Apply
template<typename _T1,typename _T2,int _n>
struct Apply {
typedef Meta <_f,_T1,_T2,_n>::Type Type;
};
};
};
};
// 下面这两行代码(#1,#2)不能通过编译,因为这时 F::Apply<>::Type 还没有定义,
// 所以不能递归地用于G::Apply<>::Type 的定义
/*
typedef typename W::Apply<f>::Type t; //#1
typedef typename G::Apply<t>::Type Type; //#2
*/
struct G3 {
template<typename f,typename T1,typename T2,int n>
struct Meta {
// 函数体
/* ... */
// 递归
typedef f::Apply<T1,T2>::Type Type;
// 或者 enum {Value = f::Apply<T1,T2>::Value }
// 这里没有计算递归终结条件,但这个不是要讨论的重点,重点是递归实现了。
};
template<_f>
struct Apply {
struct Type {
struct Apply<typename _T1,typename _T2> { //#Apply
typedef Meta <_f,_T1,_T2>::Type Type;
};
};
};
}
typedef Y<G3> func;
auto value = func::Apply<type1,type2>::Value; // 这里的func::Apply 就是 G3::Apply<>::Type::Apply<>
typedef Y<G>::Type Factorial;
typedef Y<G2>::Type Fibonacc;
std::cout << typeid(Factorial).name() << std::endl;
std::cout << typeid(Factorial::Apply<10>::Type).name() << std::endl;
std::cout << Factorial::Apply<20>::Value << std::endl;
std::cout << typeid(Fibonacc).name() << std::endl;
std::cout << typeid(Fibonacc::Apply<10>::Type).name() << std::endl;
std::cout << Fibonacc::Apply<93>::Value << std::endl;
std::cout << Fibonac(42) << std::endl;
unsigned int Fibonac(int n)
{
if(n < 2)
return n;
return Fibonac(n - 1) + Fibonac(n - 2);
}