Using void * is typical of C-reverse C interfaces to pass some "state" to a function. However, std :: function does not need this because std :: function supports "stateful functions". So you can do something like this:
double Integrate( std::function<double(double)> func, double a, double b) { typedef std::function<double(double)> fun_type; ::: F.function = [](double x, void* p){ return (*static_cast<fun_type*>(p))(x); }; F.params = &func; ::: }
and save the link to the parameter vector as part of a functor that will be encapsulated in a std :: function object or to do something like this:
void Another_function() { double m = 2; double b = 3; auto func = [&](double x){return m*x+b}; auto r1 = Integrate(func,0,3); ::: }
However, this solution will use quite a bit of guidance. GSL will reference your lambda. Your lambda will call the function std :: function <>: :(), which in turn will call some kind of virtual function, which is used to erase the type, which, in turn, will call the actual calculation.
So, if you care about performance, you can get rid of a couple of layers, in particular, std :: function. Here is another approach with a function template:
template<class Func> double Integrate( Func func, double a, double b) { ::: F.function = [](double x, void* p)->double{ return (*static_cast<Func*>(p))(x); }; F.params = &func; ::: }
I think I would prefer this by the solution of std :: function.
sellibitze
source share