几段纠结的C++代码,求解脱~

rularys rularys 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近接触些函数式编程语言,后来想起来C++好像在某个方面也支持函数式“编程”,于是找资料~ 记忆中好像有过一本书叫做什么来着~呃 哦对了,叫做《C 语言 什么来着~》作者是...谁来着??
不说这些了,我说到哪里了?哦对了,最近接触了些函数式编程语言,没事自己在C++也捣鼓了一番。这下面的几段代码是捣鼓出来了,可有根像刺一样的刺就像刺在了肉里,纠结,特冒雨上来求解脱:

首先下面是两个类似 D&A的《C++ Template Meta Programing》里面所说的“元函数类”的类,是为接着下来的类做准备的:


// 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;
};
};
};
};

貌似不太花吧;我解释一下里面的 Apply::Type::Apply::Type::Apply::Type::Apply ...
这是因为C++的模板机制不支持类似这样的特性:


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>);


请允许我偷懒用struct 来代替class,在这里用哪一个都能说明问题。

可能有人看出来了,如果把模板当作函数(元函数?),把模板参数作为其函数形参,那么上面这些特性是与 Partial evaluationhttp://en.wikipedia.org/wiki/Partial_evaluation有关的设定。哦,这与我标头里所提到的代码有关。我绕了个弯,用Apply<>::Type::Apply<>::Type::Apply<>::Type 来模拟<><><>;而且还模拟得似模似样,这多亏了我前面所提到的那本书。呃,好像我是从那里面抄来的~ 请知道的人不要声张。

哦,我记起来我要说什么了,就是像下面的一个 Y Combinator ,它只认识"元函数类":


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;
};


里面的注释就是问题所在,也是纠结所在。若是#1和#2两行代码能起作用,那么在我心目中这个Y 就算合格了。可是我的思路断了,不知如何再绕过去,所以这个Y 只能用于 第二形参是 int 的元函数类,而且必须是没有第三个形参的。OTZ 求解脱!

PS : 关于 Y Combinator,我记得函数是编程里面是位明星,所以这里不做解释了;如有兴趣,可参考这里:http://en.wikipedia.org/wiki/Y_combinator
关于 Partial evaluation ,更多的信息可参考 lambda calculus 以及 Higher-order_functions http://en.wikipedia.org/wiki/First-class_function#Higher-order_functions:_passing_functions_as_arguments
...全文
评论 22
收藏 已收藏
分享
返回顶部
rularys
C++ 语言 65,212 成员 250,513 内容