专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > C++

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

发布时间:2011-06-29 00:40:51 文章来源:www.iduyao.cn 采编人员:星星草
模板还可以这样递归...
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基础嘛
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: