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