首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-09-07 
几段纠结的C++代码,求解脱~最近接触些函数式编程语言,后来想起来C++好像在某个方面也支持函数式“编程”,于

几段纠结的C++代码,求解脱~
最近接触些函数式编程语言,后来想起来C++好像在某个方面也支持函数式“编程”,于是找资料~ 记忆中好像有过一本书叫做什么来着~呃 哦对了,叫做《C 语言 什么来着~》作者是...谁来着??
不说这些了,我说到哪里了?哦对了,最近接触了些函数式编程语言,没事自己在C++也捣鼓了一番。这下面的几段代码是捣鼓出来了,可有根像刺一样的刺就像刺在了肉里,纠结,特冒雨上来求解脱:

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

C/C++ code
// Factorial Gstruct 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 Gstruct 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++的模板机制不支持类似这样的特性:

 
C/C++ code
        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 ,它只认识"元函数类":

C/C++ code
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

[解决办法]
模板元编程,脑力过剩者的游戏。
楼主继续吧。我是不准备浪费精力在上面了。
[解决办法]
你编程是为了解决问题,不是为了研究语言,解脱个什么劲啊。
[解决办法]
探讨

呃 忘记了贴上后面的代码。这是上面那个 Y 和两个 G 的例子:
C/C++ code

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;

[解决办法]
个人意见:模版和函数式编程都是语法糖。
[解决办法]
看得头都晕了

热点排行
Bad Request.