gpt4 book ai didi

c++ - 你能举个关于这一点的例子吗 : lambda-expression

转载 作者:太空狗 更新时间:2023-10-29 20:30:43 33 4
gpt4 key购买 nike

来自 n3290 的观点:ISO 标准草案,§5.1.2/9

A lambda-expression whose smallest enclosing scope is a block scope (3.3.3) is a local lambda expression; any other lambda-expression shall not have a capture-list in its lambda-introducer. The reaching scope of a local lambda expression is the set of enclosing scopes up to and including the innermost enclosing function and its parameters. [ Note: This reaching scope includes any intervening lambda-expression's — end note ]

任何人都可以针对上述观点给出一个例子,尤其是:“其他 lambda 表达式不应在其 lambda 引入器中包含捕获列表。”这种情况出现在哪里?

最佳答案

这种情况理论上应该出现在命名空间范围内,如@Space_C0wb0y 在 his comment link 中所示。 .

#include <iostream>

int x = 12;
auto l = [&x](){ return x; };

int main() {
std::cout << l() << std::endl;
}

如果发现 GCC 接受该片段很奇怪,因为 MSVC 正确拒绝它并显示以下错误消息:

error C3480: 'x': a lambda capture variable must be from an enclosing function scope

关于c++ - 你能举个关于这一点的例子吗 : lambda-expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6111617/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com