I can't pass lambda with reference capture

You can only do the above with capture-less lambdas.

See [expr.prim.lambda.closure] (sec 7)

The closure type for a non-generic lambda-expression with no lambda-capture whose constraints (if any) are satisfied has a conversion function to pointer to function with C++ language linkage having the same parameter and return types as the closure type's function call operator.

Since lambdas are not just ordinary functions and capturing it need to preserve a state, you can not find any simple or conventional solution to make them assign to function pointers.


To fix, you can use std::function which will do it by type erasure:

#include <functional> // std::function

int test;
std::function<float(int)> f = [&](int i) -> float {return static_cast<float>(test); };

A lambda (with captures) is not the same as a function pointer, and cannot be converted to one.

A capture-less lambda can be converted to a function pointer.

See CPPReference, specifically the bit that begins:

A generic captureless lambda has a user-defined conversion function template with the same invented template parameter list as the function-call operator template.