I am playing with Class Template Deduction Guide and trying to use it recursively. But I am not able to get the following code to compile
#include <type_traits>
template<int N>
using int_const = std::integral_constant<int,N>;
template<int N>
struct Foo{
constexpr static int value = N;
template<int C>
constexpr Foo(int_const<C>){};
};
Foo(int_const<0>) -> Foo<1>;
template<int N>
Foo(int_const<N>) -> Foo<N*(Foo{int_const<N-1>{}}.value)>;
int main(){
return Foo{int_const<5>{}}.value;
}
This is the error:
<source>: In substitution of 'template<int N> Foo(int_const<N>)-> Foo<(N * > Foo{std::integral_constant<int, (N - 1)>{}}.value)> [with int N = -894]': <source>:17:51: recursively required by substitution of 'template<int N> Foo(int_const<N>)-> Foo<(N * Foo{std::integral_constant<int, (N - 1)>{}}.value)> [with int N = 4]' <source>:17:51: required by substitution of 'template<int N> Foo(int_const<N>)-> Foo<(N * Foo{std::integral_constant<int, (N - 1)>{}}.value)> [with int N = 5]' <source>:20:30: required from here <source>:17:1: fatal error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) Foo(int_const<N>) -> Foo<N*(Foo{int_const<N-1>{}}.value)>; ^~~
compilation terminated.