Convert variable-capture lambda expression to function pointer

I am trying to use lambda functions to quickly check a situation and I am running to the wall with it. I have no idea why things aren't working (I feel), they should be.

This works as I expected:

double(*example)(double) = [](double S)->double {return std::max(1-100/S, 0.0) * LogNormal(S, 100, 0.25); };
NewtonCotes(lowerBound, upperBound, example, intervals, order)

      

However, it is not:

double(*example)(double) = [K](double S)->double {return std::max(1 - K / S, 0.0) * LogNormal(S, 100, 0.25); };

      

Providing error:

Error: no suitable conversion function from "lambda [] double (double S) β†’ double" to "double (*) (double)" exists.

I don't understand why adding something to the capture list should change what is happening here. I'm new to lambdas in C ++, although maybe a silly mistake somewhere ...

What do I need to do to get this to work? I saw several people point out that there is a bug in intellisense and something like this should work, although it was a slightly different problem (at least I didn't think they were exactly the same). I am also using VS2013 and not 2011 where this bug was mentioned.

+3


source to share


1 answer


The reason for this is capture - if the lambda captures something, if it cannot be represented as a function pointer.



This makes sense if you think that in order to capture any variables, the compiler must create a new type that will store the captured variables and provide non operator()

- static ones - so the object returned by the lambda expression has state and cannot be converted to a simple function pointer.

+3


source







All Articles