.
Hi :-)
I have the following code : the goal is to return a function that is the sum of other functions, roughly. And to learn about variadic templates.
#include <iostream>
template <typename F>
struct FSum {
FSum(F f) : f_(f) {} ;
int operator()() {
return f_() ;
}
F f_ ;
} ;
template <typename F1, typename F2, typename... Frest>
struct FSum {
FSum(F1 f1, F2 f2, Frest... frest) {
f_ = f1 ;
frest_ = FSum<F2, Frest...>(f2, frest...) ;
}
int operator()() {
return f_() + frest_() ;
}
F1 f_ ;
FSum<F2, Frest...> frest_ ;
} ;
struct F1 {
int operator() () { return 1 ; } ;
} ;
struct F2 {
int operator() () { return 2 ; } ;
} ;
struct F3 {
int operator() () { return 3 ; } ;
} ;
int main() {
F1 f1 ;
F2 f2 ;
F3 f3 ;
FSum<F1, F2, F3> fsum = FSum<F1, F2, F3>(f1, f2, f3) ;
std::cout << fsum() << std::endl ;
}
But I got the following error message from clang (g++ also gives an error) :
test_variadic.cpp:14:1: error: too many template parameters in template redeclaration template ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
And well I don't understand. I though the compiler would choose the class depending on the number of template parameters ? Since the first one has exactly one and the other one has 2 or more.
Any idea ?
Thanks a lot :-)