There are two pointers to different functions
typedef std::vector<double> TData;
double ( * p_test1 ) ( const TData> &arg);
double ( * p_test2 ) ( const TData> &ar1, char *arg2, char *arg3);
and a method which has as an argument a pointer to the function
double f (double ( * p_test1 ) ( const TData &arg ))
{
//Long and not trivial algorithm processing results of p_test
for ( i = 0;... )
{
double res = p_test (arg); //Some computations
}
}
The f() method contains difficult calculations (here replaced by a for cycle).
Is it possible to templatize this argument (i.e., pointer to a function having different amount of parameters) to get a general function processing both types arguments
double f (double ( * p_test1 ) ( const TData &arg ));
double f (double ( * p_test2 ) ( const TData> &ar1, char *arg2, char *arg3));
Or is there any way how to write such a function, for example to write a pointer to a pointer to the function?
I would like to avoid the partial specialization of f() function because of its complexity (repetitively overwritten of the long code is not efficient).
Thanks for your help…
A method that can take anything can, as a special case, also take function pointer. E.g.
The problem however gets down to the fact, that the two functions are taking different arguments. So the arguments either have to come somehow bundled to
f, there need to be several different implementations anyway, or the arguments will always be the same.To bundle the arguments, usual method is to use
std::bind(C++11) orboost::bind. Say you have a function that needs 3 arguments (test2) and need to pass it to generic algorithm (f) that will only provide the first. And you know the other 2. So you do:(In C++11
bindisstd::bindand_1isstd::placeholders::_1, in Boostbindisboost::bindand_1is in anonymous namespace provided by the header.) In this casefneeds to take any argument, because the return type ofbindis unspecified class type with appropriateoperator().