这段代码是否正确?
std::function<int(int)> f = [&f](int n) -> int
{
return n <= 1 ? 1 : n * f(n - 1);
};
int x = f(42);
对象构造在作为引用传递给 lambda 之前是否存在任何潜在问题?或者这段代码绝对正确?
按值捕获 f 会导致 msvc2010 编译器崩溃。
最佳答案
这应该可以正常工作,只要您遵循对存储在 lambda 中的堆栈变量的引用规则。它是定义明确的 C++11 代码。
关于c++ - 通过引用递归 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10046035/