How do I create a 'reference' to a lambda?

c++, c++11, lambda

Solution

Since the lambda is not stateless, it cannot be converted to a function pointer. Use `std::function` instead.

std::function<int(int)> factorial  = [&](int x){
  return (x < 2)
      ? 1
      : x * factorial(x - 1);
};

Problem

I want to capture a 'reference' to a lambda, and I thought that a function pointer would do the trick, as in: ``` int (*factorial)(int) = [&](int x){ return (x < 2) ? 1 : x * factorial(x - 1); }; ``` but I get `cannot convert from main::lambda<......> to int(_cdecl *)(int)`. What's the proper way to point to a lambda then?

Original source

Related problems