So i\'m trying to write an Integration function to be used with c++11 lambdas. The code looks something like this:
double Integrate(std::function
Looks like the gsl library requires a function pointer. A lambda which does not capture can be converted to a function pointer. Any lambda can be converted to a std::function
. But a std::function
cannot be converted to a function pointer.
You could try:
struct functor_and_params {
std::function f;
void* params;
static double invoke(double x, void* ptr) {
functor_and_params& f_and_p = *reinterpret_cast(ptr);
return f_and_p.f(x, f_and_p.params);
}
};
double Integrate(std::function func,
double a,double b,std::vector & params) {
functor_and_params f_and_p{ func, ¶ms };
gsl_function F;
F.function = &functor_and_params::invoke;
F.params = &f_and_p;
//...
}