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<double(double,void*)> func, double a,double b,std::vector<double> & params) { gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000); gsl_function F; F.function =func; F.params = (void*)¶ms; double error,result; gsl_integration_qag (&F, a, b, 0, 1e-7, 1000,GSL_INTEG_GAUSS61,w, &result, &error); gsl_integration_workspace_free (w); return result; } void Another_function() { //... Integrate([](double a,void* param) { return ((vector<double> *)params)->at(0)*a+((vector<double> *)params)->at(1); } ,0,3,{2,3}); } Trying to compile this, compiler says:
error: cannot convert ‘std::function<double(double, void*)>’ to ‘double (*)(double, void*)’ in assignment about line
F.function =func; But if I write:
F.function =[](double a,void* param) { return ((std::vector<double> *)param)->at(0)*a+((std::vector<double> *)param)->at(1); }; It compiles and works fine. How should I solve this?
std::function? Could you change the first parameter ofIntegerateto be a function pointer? Because there's no way you're going to be able to use astd::functionas a function pointer, unless you get into some very ugly global variable business. Note that you can store a lambda in a function pointer, as long as it doesn't capture, which, in the example you showed, it does not.void*parameter to be passed through.