Question about functions

Hi everyone!

I have the following problem: How can I write a function g that takes one parameter of type double, call it x, and returns a pointer to the following function which depends on this parameter x:
1
2
3
double f(double y){
return std::exp(y)-x;
}

Any ideas how this can be implemented?
1
2
3
4
5
6
//requires C++11/0x support
//requires <functional>
std::function<double(double)> g(double x)
{
      return [x](double y){return std::exp(y)-x;};
}


If your compiler doesn't support std::function you can also use a regular function pointer - but then you would have to allocate the lambda on heap.


Last edited on
Topic archived. No new replies allowed.