这个 lambda 递归是否有效?
#include #include int main() {std::functiong = [&g](int k) {返回 (k ? k * g(k-1) : 1);};std::cout <<克(10);//3628800}
它似乎可以编译和运行,但我对关闭 g
感到紧张与我初始化它的语句相同.在 1-10 的范围内严格效度...?
在您通过引用捕获 g
时,它已被声明,因此该名称可供使用:><块引用>
3.3.2/1 名称的声明点紧跟在其完整声明符(第 8 条)之后和初始化符之前
您可以在初始化之前以有限的方式使用对象 - 基本上,任何不依赖于值的东西都可以:
<块引用>3.8/6 在对象的生命周期开始之前但在对象将占用的存储空间之后已分配 [...] 任何引用原始对象的泛左值都可以使用,但仅限于有限的方法.[...] 使用不依赖于其值的泛左值的属性是明确定义的.
所以根据我的理解,你正在做的事情是明确定义的.
(虽然是超迂腐,我不认为它是在分配自动对象的存储时指定的,并且 8.3.2/5 说应初始化引用以引用有效对象"而没有定义有效",所以有理由认为它没有明确定义).
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...?
At the point at which you capture g
by reference, it has been declared, so the name is available for use:
3.3.2/1 The point of declaration for a name is immediately after its complete declarator (Clause 8) and before its initializer
You are allowed to use objects in limited ways before they are initialised - basically, anything that doesn't depend on the value is OK:
3.8/6 before the lifetime of an object has started but after the storage which the object will occupy has been allocated [...] any glvalue that refers to the original object may be used but only in limited ways. [...] using the properties of the glvalue that do not depend on its value is well-defined.
So by my understanding, what you are doing is well-defined.
(Although, being ultrapedantic, I don't think it's specified when the storage for an automatic object is allocated, and 8.3.2/5 says that "a reference shall be initialized to refer to a valid object" without defining "valid", so there's scope to argue that it's not well-defined).
这篇关于对于 lambda 来说,本质上来说,它自己关闭是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!