使用以下代码,我收到编译错误 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
}();
但是,以下代码可以编译:
However, the following code compiles:
[] {
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 隐式捕获因从结构化绑定声明的变量而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!