I have a question about using lambda expression as input for another function. In the following, I reproduce the problem using the simplest code:
#include<iostream>
using namespace std;
double myop(double (*func)(double), double x)
{
return (*func)(x);
}
int main()
{
double a = 0.1;
auto sq = [a](double x)
{
return a+x*x;
};
double x = 2;
cout << myop(sq, x) << endl;
return 0;
}
Once I compile the code, the compiler pop up "error: cannot convert ‘main()::’ to ‘double ()(double)’ for argument ‘1’ to ‘double myop(double ()(double), double)’ cout << myop(sq, x) << endl;".
I have tested that if the lambda expression doesn't contain any external parameters (such as 'a' in this example), then it works. But how can I deal with the case that having some external parameter dependence? Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire