考虑这段代码:
#include <iostream>
typedef long xint;
template<int N>
struct foz {
template<int i=0>
static void foo(xint t) {
for (int j=0; j<10; ++j) {
foo<i+1> (t+j);
}
}
template<>
static void foo<N>(xint t) {
std::cout << t;
}
};
int main() {
foz<8>::foo<0>(0);
}
在使用 clang++ -O0
进行编译时,它可以在几秒钟内完成编译,然后运行 4 秒钟。
然而,使用 clang++ -O2
进行编译需要很长时间和大量内存。在 Compiler Explorer 上可以看到,将 8
更改为较小的值后,它会完全展开循环。
我不打算完全不优化,但是想让它不递归,就像嵌套循环应该表现的那样。我需要做些什么吗?