43
Is this lambda recursion valid?
#include <functional>
#include <iostream>
int main() {
std::function<int(int)> g = [&g](int k) {
return (k ? k * g(k-1) : 1);
};
std::cout << g(10); // 3628800
}
It appears to compile and run ok, but I'm nervous about closing over g in the same statement that I initialise it. Strict validity on a scale of 1-10...?