读书人

模板还可以这样递归.解决办法

发布时间: 2012-03-01 10:25:46 作者: rapoo

模板还可以这样递归...

C/C++ code
#include <iostream>using namespace std;const unsigned P = 10;template <unsigned P> struct test{    static void func()    {        cout << "func: " << P << endl;        test<P - 1>().func();    }}; struct test<0>{    static void func()    {        cout << "func0" << endl;    }};int main(){    test<P>::func();    return 0;}


[解决办法]
不错,有机会也来用看看
[解决办法]
模板...最近想开始学了..
[解决办法]
C++模板元编程,一直没看。

[解决办法]
模板,我一直很头疼的东东

[解决办法]
这是 元编程 的其中的一个基础。
[解决办法]
貌似挺用的~
[解决办法]
C/C++ code
#include <iostream>using namespace std;const unsigned P = 10;template <unsigned P>struct test{    static void func()    {        cout << "func: " << P << endl;        test<P - 1>::func();//这里直接使用::操作符调用就行了,没必要使用对象调用func静态成员函数了    }};template<>//这里应该是特化struct test<0>{    static void func()    {        cout << "func0" << endl;    }};int main(){    test<P>::func();    return 0;}
[解决办法]
貌似挺用的~
[解决办法]
学习了
[解决办法]
MPL基础嘛

读书人网 >C++

热点推荐