I just learned of a neat trick for getting a function pointer from a lambda in C++. This is not my code but I am passing it along for others should you happen upon my humble blog.

#include <cstdio>
#include <typeinfo>
int main ()
    auto x = [] {};
    auto y = +x;
    auto z = +[] {};
    std::printf ("%s %p\n", typeid (x).name (), x);
    std::printf ("%s %p\n", typeid (y).name (), y);
    std::printf ("%s %p\n", typeid (z).name (), z);


$ ./test | c++filt -t
main::{lambda()#1} 0x7ffde2731538
void (*)() 0x401131
void (*)() 0x40115d

You can also use this trick with say decltype(+x) (using the above snippet for context) to generate a function pointer type.

Importantly, if you add a capture to x’s declaration a compile error will occur on y.