Lambda implicit capture fails with variable declared from structured binding(Lambda 隐式捕获因结构化绑定声明的变量而失败)
问题描述
使用以下代码,我得到一个编译错误 C2065 'a': undeclared identifier
(使用 Visual Studio 2017):
With the following code, I get a compile error C2065 'a': undeclared identifier
(using visual studio 2017):
[] {
auto [a, b] = [] {return std::make_tuple(1, 2); }();
auto r = [&] {return a; }(); //error C2065
}();
但是,以下代码编译:
[] {
int a, b;
std::tie(a, b) = [] {return std::make_tuple(1, 2); }();
auto r = [&] {return a; }();
}();
我认为这两个样本是等价的.是编译器错误还是我遗漏了什么?
I thought that the two samples were equivalent. Is it a compiler bug or am I missing something ?
推荐答案
核心问题 2313 改变了标准,使得结构化绑定永远不是变量的名称,因此它们永远不可捕获.
Core issue 2313 changed the standard so that structured bindings are never names of variables, making them never capturable.
P0588R1的改写的 lambda 捕获措辞明确了这一禁令:
P0588R1's reformulation of lambda capture wording makes this prohibition explicit:
如果 lambda 表达式 [...] 捕获结构化绑定(显式或隐含地),程序格式错误.
If a lambda-expression [...] captures a structured binding (explicitly or implicitly), the program is ill-formed.
请注意,这个措辞应该是一个占位符,而委员会会弄清楚这种捕获应该如何工作.
Note that this wording is supposedly a placeholder while the committee figures out exactly how such captures should work.
由于历史原因保留了以前的答案:
这在技术上应该可以编译,但是这里的标准中有一个错误.
This technically should compile, but there's a bug in the standard here.
标准规定 lambda 只能捕获变量.它说非元组的结构化绑定声明不会引入变量.它引入了名称,但这些名称不是变量的名称.
The standard says that lambdas can only capture variables. And it says that a non-tuple-like structured binding declaration doesn't introduce variables. It introduces names, but those names aren't names of variables.
另一方面,类似元组的结构化绑定声明确实引入了变量.a
和 b
在 auto [a, b] = std::make_tuple(1, 2);
是实际的引用类型的变量.所以它们可以被 lambda 捕获.
A tuple-like structured binding declaration, on the other hand, does introduce variables. a
and b
in auto [a, b] = std::make_tuple(1, 2);
are actual
reference-typed variables. So they can be captured by a lambda.
显然,这不是一个理智的状态,委员会知道这一点,因此应该会尽快修复(尽管在捕获结构化绑定的确切方式上似乎存在一些分歧).
Obviously this is not a sane state of affairs, and the committee knows this, so a fix should be forthcoming (though there appears be some disagreement over exactly how capturing a structured binding should work).
这篇关于Lambda 隐式捕获因结构化绑定声明的变量而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Lambda 隐式捕获因结构化绑定声明的变量而失败
基础教程推荐
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01