いんすぱいあモトはコチラ → 再帰処理
#include <iostream>
template<int N>
struct factorial {
const static int value = N * factorial<N-1>::value;
};
template<>
struct factorial<0> {
const static int value = 1;
};
int main() {
std::cout << factorial<5>::value << std::endl;
}
templateの真骨頂、なのかな。
特殊化によって無限再帰に終止符を打てるわけだ。
※ この再帰は(実行時ではなく)コンパイル時に展開されます。