I'm trying to code a numeric method that can accept a function as an argument that itself has arbitrary arguments. The best way to do this seems to be with a variadic template. This https://stackoverflow.com/a/15960517/3787488 answer is nearly exactly what I need, but my code wont compile.
This is my test case;
#include<iostream>
#include<vector>
#include<fstream>
#include<functional>
#include<iomanip>
double testfunction(double x, double k);
template<typename... Ts>
using custom_function_t = double(*) (double, Ts...);
template< typename... Ts>
double test( custom_function_t<Ts...> f, Ts... args, double min, double max, int m, int n)
{
double ans=0;
double step=(max-min)/100.00;
for (double x=min;x<=max;x=x+(max-min)/100)
{
ans=ans+(step/6.0)*(f(x, args...)+4*f(x+0.5*step, args...)+f(x+step, args...));
}
return(ans);
}
int main()
{
double ans=0;
std::cout<<test(testfunction,2.0,0.0,1.0,0,0)<<endl;
return(0);
}
double testfunction(double x, double k)
{
double ans=0;
ans=x*x*k;
return(ans);
}
Where the function 'test' should take the function 'testfunction' and numerically integrate it (integration of 2*x^2 from 0 to 1=2/3).
Compiling with gcc 4.7.3 c++11 I get the errors;
note: template<class ... TS> double test (custom_function_t<Ts ...>, Ts ..., double, double, int, int)
note: template argument deduction/substitution failed:
note: candidate expects 5 arguments, 6 provided