【发布时间】:2011-03-04 00:45:37
【问题描述】:
我正在尝试使用模板在 C++ 中展开循环,如下所示。
#include <iostream>
template< class T, T i >
struct printDown {
static void run(void) {
std::cout << i << "\n";
printDown< T, i - 1 >::run();
}
};
template< class T >
struct printDown< T, 0 > {
static void run(void) {
std::cout << 0 << "\n";
}
};
int main(void) {
printDown< int, 10 >::run();
return 0;
}
当我在 Cygwin 中使用 g++ 3.4.4 编译时,出现以下错误。
tmp.cpp:12: error: type
T' of template argument0' 取决于 模板参数
我做错了什么?我需要以某种方式注释 0 以说明它是 T 类型吗?
提前致谢。
【问题讨论】:
标签: c++ templates specialization partial-specialization loop-unrolling