Can lambda functions be recursive? [duplicate]
Yes, they can. You can store it in a variable and reference that variable (although you cannot declare the type of that variable as auto
, you would have to use an std::function
object instead). For instance:
std::function<int (int)> factorial = [&] (int i)
{
return (i == 1) ? 1 : i * factorial(i - 1);
};
Otherwise, no, you cannot refer the this
pointer from inside the body of the lambda.