Quick answer: Yes, use std::function
.
This recent short question shows a concise and correct answer, along with a link to a previous more complex example.
Can lambda functions be recursive?
Here is a plain old recursive function:
int fak(int n) { return (n <= 1) ? 1 : n * fak(n - 1); }How would I write such a recursive function as a lambda function?
[](int n) { return (n <= 1) ? 1 : n * operator()(n - 1); } // error: operator() not defined [](int n) { return (n <= 1) ? 1 : n * (*this)(n - 1); } // error: this wasn't captured for this lambda functionIs there any expression that denotes the current lambda so it can call itself recursively?
Add a Comment
Comments are closed.