使用C++中的模板展开循环,并进行部分特化

Ash*_*ley 6 c++ templates partial-specialization specialization loop-unrolling

我正在尝试使用模板在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;
}
Run Code Online (Sandbox Code Playgroud)

当我在Cygwin中编译w/g ++ 3.4.4时,我收到以下错误.

tmp.cpp:12:错误:类型T' of template argument0'取决于模板参数

我究竟做错了什么?我是否需要以某种方式注释0来说它是T型?

提前致谢.

pho*_*oji 5

你尝试过int i而不是T i吗?