更新时间:2023-11-11 15:12:16
核心问题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.
类似元组的结构化绑定声明,确实引入了变量. auto [a, b] = std::make_tuple(1, 2);
中的a
和b
是实际的
引用类型的变量.因此它们可以被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).