【问题标题】:Lambda capture, initializers and nested structLambda 捕获、初始化器和嵌套结构
【发布时间】:2020-05-23 10:10:16
【问题描述】:

有人能解释一下这里发生了什么吗(GCC 7.3):

#include <thread>
#include <iostream>

struct A
{
    struct B {};
};

int main()
{
    int var = 0;
    std::thread([c=A::B(), var](){ });     // error: ‘var’ was not declared in this scope
    std::thread([c=A(), var](){ });        // OK
    std::thread([c=A::B(), var=var](){ }); // OK
    return 0;
}

当我捕获嵌套结构时,我得到了:

'var' 未在此范围内声明

另一方面,捕获非嵌套结构是可行的。也可以通过初始化进行捕获。此外,所有案例都可以在 Visual Studio 中使用。

【问题讨论】:

  • 使用最近的编译器...
  • Here 你可以测试很多 G++ 版本。从g++8.1 开始工作。如果您想继续使用 g++7.x,只需颠倒顺序:[var, c=A::B()] - working

标签: c++ gcc lambda c++14 gcc7


【解决方案1】:

必须是 GCC 7.x 中的错误,因为 8.1 接受它。 https://godbolt.org/z/xXw6qN

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多