繁体   English   中英

Lambda 捕获、初始值设定项和嵌套结构

[英]Lambda capture, initializers and nested struct

有人可以解释这里发生的事情(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。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM