模板元编程递归上限? [英] Template metaprogramming recursion up limits?

查看:53
本文介绍了模板元编程递归上限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用元编程编写一个非常简单的模板类,以在编译时计算总和,如下所示:

I am writing a very simple template class using Metaprogramming to compute sum in compile time, as below:

#include <iostream>

using namespace std;

template<int N>
class Sum
{
    public:
        enum {value = N + Sum<N-1>::value };
};

template<>
class Sum<0>
{
    public:
        enum {value = 0};
};


int main()
{
    cout << Sum<501>::value << endl;
}

有趣的是:

  • 当我打印Sum< 500>以下时,效果很好
  • 当涉及Sum< 501>时,编译失败并显示:

  • When I print Sum<500> and below, it works fine
  • When it comes to Sum<501>, the compile failed with:

sum.cpp:9:实例化为 Sum< 500>'sum.cpp:9:实例化来自 Sum< 501>'sum.cpp:22:从此处实例化

sum.cpp:9: instantiated from Sum<500>' sum.cpp:9: instantiated fromSum<501>' sum.cpp:22: instantiated from here

sum.cpp:9:错误:类型 Sum< 1>'不完整用于嵌套名称说明符sum.cpp:9:错误: value'的枚举器值不是整数恒定

sum.cpp:9: error: incomplete type Sum<1>' used in nested name specifier sum.cpp:9: error: enumerator value forvalue' not integer constant

  • Sum< 501>将报告Sum< 1>的错误,Sum< 502>将报告Sum< 2>的错误,差异始终为2,在我看来,编译器的极限资源为500.

  • Sum<501> will report error of Sum<1>, Sum<502> will report error of Sum<2>, the difference is always 2, it seems to me the compiler has a limit resource of 500.

    对此有任何想法吗?他们是否有打破这种限制的方法?

    Any idea about this? and is their a way to break this limits?

    谢谢.


    谢谢大家,重点不是算法,而是编译器的局限性-我知道有一种简单的方法可以求和:)


    Thanks guys, the point is not about the algorithm, but rather the compiler limitation - I know there is an easy way to get sum:)

    Edit2:

    • 使用gcc 4.6 +时,错误消息会更有帮助
    • Use gcc 4.6 +, the error message is much more helpful

    sum.cpp:9:14:错误:模板实例化深度超过的最大值实例化1024(使用-ftemplate-depth =来增加最大值)"class Sum< 1>" sum.cpp:9:14:从递归实例化"Sum< 1024>" sum.cpp:9:14:从"Sum< 1025>"实例化sum.cpp:22:22:从此处实例化

    sum.cpp:9:14: error: template instantiation depth exceeds maximum of 1024 (use -ftemplate-depth= to increase the maximum) instantiating ‘class Sum<1>’ sum.cpp:9:14: recursively instantiated from ‘Sum<1024>’ sum.cpp:9:14: instantiated from ‘Sum<1025>’ sum.cpp:22:22: instantiated from here

  • 所以是的,使用ftemplate-depth是正确的方法.但是在Windows中呢?VC9.0的上限是499,似乎没有设置模板深度的选项,请参见

    so yes, use ftemplate-depth is the right way. But how about in windows? the uplimits for VC9.0 is 499, and seems there is no option to set the template depth, see here

    推荐答案

    如果使用的是GCC,则可以使用 -ftemplate-depth = X 设置模板递归深度,其中 X是必需的深度:

    If you are using GCC, you can set the template recursion depth with -ftemplate-depth=X, where X is the required depth:

    g++ ...... -ftemplate-depth=750
    

    请记住,这不仅是您可以任意设置较高的限制.在某些时候,您会遇到操作系统和硬件限制.

    Bear in mind that this is not just some limit that you can set arbitrarily high. At some point you will run into OS and hardware limitations.

    关于您的实际求和函数,有一个众所周知的解析方法,用于求解前N个正整数的和.

    Concerning your actual sum function, there is a well known analytical solution to the Sum of the first N positive integers.

    (即 n *(n + 1)/2 )

    这篇关于模板元编程递归上限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆