I run f<5>() and get the error log shown below:
template<bool B, typename T>
using Enable_if = typename std::enable_if<B,T>::type;
template<int N>
Enable_if<(N>0),void> f(){
std::cout << N;
f<N-1>();
}
template<int N>
Enable_if<(N==0),void> f(){
return;
}
In instantiation of 'Enable_if<(N > 0), void> f() [with int N = 1; Enable_if<(N > 0), void> = void]':
13:12: recursively required from 'Enable_if<(N > 0), void> f() [with int N = 3; Enable_if<(N > 0), void> = void]'
13:12: required from 'Enable_if<(N > 0), void> f() [with int N = 4; Enable_if<(N > 0), void> = void]'
25:8: required from here
13:12: error: no matching function for call to 'f()'
13:12: note: candidate is:
11:23: note: template<int N> Enable_if<(N > 0), void> f()
11:23: note: template argument deduction/substitution failed:
The code below runs fine with f<5>() and prints 543210:
template<int N>
void f(){
std::cout << N;
f<N-1>();
}
template<>
void f<0>(){
std::cout << 0;
}
It is taken from Stroustrup(C++11) page 845.