编译递归模板以调用静态成员函数时类型不完整

Incomplete type when compiling recursive template to call static member function

本文关键字:类型 函数 静态成员 递归 调用 编译      更新时间:2023-10-16

我正在Windows上使用g ++版本3.4.4编译此代码:-

#include <iostream>
template< int i >
class LOOP{
 public:
    static inline void EXEC(int* count){
        (*count)++;
            LOOP< i-1 >::EXEC(count);
   }
};
template<> class LOOP< 0 >{
  public:
    static inline void EXEC(int* count){
   (*count)++;
   }
};
 int main(int i){
int barely = 0;
LOOP< 1000 >::EXEC(&barely);
 }

它抱怨,在嵌套名称说明符中使用的不完整类型LOOP<500>,并且之前有一个先前实例的列表,"从静态void LOOP::EXEC(int *)实例化,带有i - 1000"等等。

当我将其更改为 LOOP<100 时>它编译得很好。

编辑 如果这会影响实现限制,我正在 cygwin 上运行它。

您达到了实现的模板深度限制。您可以通过使用 -ftemplate-depth=1005(现代 GCC)或-ftemplate-depth-1005(旧版 GCC)进行编译来增加限制。