The parametric function Templated Sum (Args ...) does not compile

I used a static member trick to force compilation of step 2 and still get the error message:

struct S { template <typename T> static T Sum(T t) { return t; } template <typename T, typename ... Rest> static auto Sum(T t, Rest... rest) -> decltype(t + Sum(rest...) ) { return t + Sum(rest...); } }; int main() { auto x = S::Sum(1,2,3,4,5); } 

main.cpp: 17: 14: There is no corresponding function to call "Sum"

+7
c ++ templates variadic-templates
source share
1 answer

Even when using clang 4.0 compilation fails.

I managed to compile it with decltype(auto) (only for auto will work too) instead of an explicit return type of tail.

 struct S { template <typename T> static T Sum(T t) { return t; } template <typename T, typename ... Rest> static decltype(auto) Sum(T t, Rest... rest) { return t + Sum(rest...); } }; 

I think that the compiler is not able to infer the type, because the output depends only on the recursive operator return.

More info here

+4
source share

All Articles