【发布时间】:2011-07-13 13:09:28
【问题描述】:
我在这里找到了一个代码Printing 1 to 1000 without loop or conditionals
有人能解释一下编译时递归是如何工作的吗,在 google 中找不到
// compile time recursion
template<int N> void f1()
{
f1<N-1>();
cout << N << '\n';
}
template<> void f1<1>()
{
cout << 1 << '\n';
}
int main()
{
f1<1000>();
}
谢谢!
【问题讨论】:
-
其实有个窍门,特化是有条件的,虽然没有
if关键字... -
有没有比运行时递归快得多的经验法则?
-
用它代替常规递归有什么好处?
标签: c++ compile-time