I am trying to pass a custom lambda to a function that expects a function pointer (more precisely the zero
function in Brent library).
The idea is that I would create the lambda once with the parameters and then it would be evaluated at several values x
inside this function.
I have tried the steps in this thread with no success and I am getting an error of no known conversion for argument 4 from ‘Function {aka std::function<double(double)>}’ to ‘double (*)(double)’
. As far as I understand the compiler does not know how to cast from those 2 types.
Is there a workaround around this error? It would be better if no modifications had to be made to the library and it could be solved within my program. Here is a snippet of code to show the problem.
# include <functional>
# include "brent.hpp"
using namespace brent;
typedef std::function<double(double)> Function;
Function function_builder (double a, double b)
{
return [a,b](double x) {
return ( a * x + b );
};
}
int main ( )
{
Function func = function_builder ( 2.0, -1.0 );
double z = zero (0, 1, 0.001, func ); //func should be a function pointer of type double (*)(double)
return 0;
}