I recently had a need for a recursive C++ lambda that captures variables. This isn't possible in the intuitive way; you get:
error: variable 'someVar' declared with deduced type 'auto' cannot appear in its own initializer.
It turns out you can achieve this (albeit a bit horribly) by passing the lambda to itself! See this article for details: https://artificial-mind.net/blog/2020/09/12/recursive-lambdas
error: variable 'someVar' declared with deduced type 'auto' cannot appear in its own initializer.
It turns out you can achieve this (albeit a bit horribly) by passing the lambda to itself! See this article for details: https://artificial-mind.net/blog/2020/09/12/recursive-lambdas

Recursive Lambdas in C++
auto fib = [](int n) { if (n <= 1) return n; return fib(n - 1) + fib(n - 2); }; auto i = fib(7); If only it were that simple. Obviously, any performance-conscious programmer will compute Fibonacci numbers iteratively (or even explicitly), but this solution will serve as an