Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Lambda捕获、初始值设定项和嵌套结构_C++_Gcc_Lambda_C++14_Gcc7 - Fatal编程技术网

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

C++ Lambda捕获、初始值设定项和嵌套结构,c++,gcc,lambda,c++14,gcc7,C++,Gcc,Lambda,C++14,Gcc7,有人能解释一下这里发生了什么(GCC 7.3): #包括 #包括 结构A { 结构B{}; }; int main() { int-var=0; std::thread([c=A::B(),var](){});//错误:未在此作用域中声明“var” std::thread([c=A(),var](){});//确定 std::thread([c=A::B(),var=var](){});//确定 返回0; } 当我捕获嵌套结构时,我得到: “var”未在此作用域中声明 另一方面,捕获非嵌套结构

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

#包括
#包括
结构A
{
结构B{};
};
int main()
{
int-var=0;
std::thread([c=A::B(),var](){});//错误:未在此作用域中声明“var”
std::thread([c=A(),var](){});//确定
std::thread([c=A::B(),var=var](){});//确定
返回0;
}
当我捕获嵌套结构时,我得到:

“var”未在此作用域中声明


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

在GCC 7.x中肯定是一个bug,因为8.1接受它

使用最近的编译器。。。您可以测试许多G++版本。从
g++8.1
开始工作。如果您想继续使用g++7.x,只需颠倒顺序:
[var,c=A::B()]
-。
#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;
}