If I write a compilation time factorial function using specialization, the following code may be sufficient and will correctly provide 120 as a result of fact1<5>() :
template <size_t N> constexpr size_t fact1() { return N*fact1<N-1>(); } template <> constexpr size_t fact1<0>() { return 1; }
However, with one function body and ternary operator, as in the following code, g ++ 4.7 and Clang ++ 3.2 both exceed the maximum depth of the template increment. It seems like 1 never returns from fact2 . Why does this definition of fact2<5>() not return 120?
template <size_t N> constexpr size_t fact2() { return N==0 ? 1 : N*fact2<N-1>(); }
source share