1

有人可以解释这里发生了什么(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。

4

1 回答 1

2

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

于 2020-02-07T09:39:27.800 回答