I was doing a quick experiment with lambda functions and I'm having trouble figuring out how to declare the testFunc
variable below.
Generally speaking, a function pointer can be defined as follows:
int (*someFunc)(int, int) = otherFunction;
When doing this for a lambda function without any closures, it works fine:
int (*someFunc)(int) = [](int a) -> int { return 0; };
However, I get a compiler error when trying to capture by reference:
void (*testFunc)() = [&]() -> void { /* code here */ };
It works if I declare testfunc
as auto
instead, but I'm curious what's wrong with the above code?