Lambda 隐式捕获因结构化绑定声明的变量而失败

Lambda implicit capture fails with variable declared from structured binding(Lambda 隐式捕获因结构化绑定声明的变量而失败)

本文介绍了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.

另一方面,类似元组的结构化绑定声明确实引入了变量.abauto [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 隐式捕获因结构化绑定声明的变量而失败

基础教程推荐