c++ - Lambda 捕获、初始化器和嵌套结构
问题描述
有人可以解释这里发生了什么(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
推荐阅读
- c++ - 动态类型转换为变量类型
- node.js - nodejs 中的 qr-scanner 模块出现错误 ReferenceError: Worker is not defined
- android - 为什么Android12源码库中找不到一些新的API,比如CompanionDeviceManager.startObservingDevicePresence
- html - 为什么这些栏在 PageSpeed 中看起来不同?
- python - AttributeError:“客户端”对象没有属性“命令”“模块@client.command() 中的第 6 行
- java - 表示字节到字节数组的字符串
- excel - VBA excel 无法通过 Internet Explorer 登录网站
- ios - 与 iCloud 的跨设备数据同步是否需要 Core Data?
- c++ - 使用 Eigen3 快速求解稀疏正定线性系统
- azure - BrowserStack 和 Azure 集成和运行测试