I want to be able to pass a variadic number of function pointers to a template function, say foo
. The example below shows what I have so far however it does not compile when I actually pass more than one template argument:
#include <iostream>
#include <cmath>
using FPtrType = double(*)(double);
constexpr double Identity( double x ) noexcept { return x; }
template <FPtrType Func=Identity>
constexpr double foo( double x ) noexcept( noexcept( Func(x) ) )
{
return Func(x);
}
template <FPtrType Func, typename... Funcs>
constexpr double foo( double x, Funcs... funcs )
{
x = Func(x);
return foo<Funcs...>(x, funcs...);
}
int main()
{
double x{ 0.5 };
std::cout << x << '\t' << foo(x) << std::endl; // OK
std::cout << x << '\t' << foo<std::sin>(x) << std::endl; // OK
std::cout << x << '\t' << foo<std::asin>(x) << std::endl; // OK
std::cout << x << '\t' << foo<std::sin, std::asin>(x) << std::endl; // Error!
}
I'm using gcc5 and the compiler spews:
error: no matching function for call to 'foo(double&)'
std::cout << x << '\t' << foo<std::sin, std::asin>(x) << std::endl;
^
, followed by another error message:
error: wrong number of template arguments (2, should be at least 0)
std::cout << x << '\t' << foo<std::sin, std::asin>(x) << std::endl;
^
Any thoughts?