<i id='xYslE'><tr id='xYslE'><dt id='xYslE'><q id='xYslE'><span id='xYslE'><b id='xYslE'><form id='xYslE'><ins id='xYslE'></ins><ul id='xYslE'></ul><sub id='xYslE'></sub></form><legend id='xYslE'></legend><bdo id='xYslE'><pre id='xYslE'><center id='xYslE'></center></pre></bdo></b><th id='xYslE'></th></span></q></dt></tr></i><div id='xYslE'><tfoot id='xYslE'></tfoot><dl id='xYslE'><fieldset id='xYslE'></fieldset></dl></div>

  1. <legend id='xYslE'><style id='xYslE'><dir id='xYslE'><q id='xYslE'></q></dir></style></legend>

    <small id='xYslE'></small><noframes id='xYslE'>

    • <bdo id='xYslE'></bdo><ul id='xYslE'></ul>
    <tfoot id='xYslE'></tfoot>

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

      时间:2023-06-30
          <tbody id='4bW9f'></tbody>
        <i id='4bW9f'><tr id='4bW9f'><dt id='4bW9f'><q id='4bW9f'><span id='4bW9f'><b id='4bW9f'><form id='4bW9f'><ins id='4bW9f'></ins><ul id='4bW9f'></ul><sub id='4bW9f'></sub></form><legend id='4bW9f'></legend><bdo id='4bW9f'><pre id='4bW9f'><center id='4bW9f'></center></pre></bdo></b><th id='4bW9f'></th></span></q></dt></tr></i><div id='4bW9f'><tfoot id='4bW9f'></tfoot><dl id='4bW9f'><fieldset id='4bW9f'></fieldset></dl></div>

        <small id='4bW9f'></small><noframes id='4bW9f'>

            • <bdo id='4bW9f'></bdo><ul id='4bW9f'></ul>

                <tfoot id='4bW9f'></tfoot>
              1. <legend id='4bW9f'><style id='4bW9f'><dir id='4bW9f'><q id='4bW9f'></q></dir></style></legend>
                本文介绍了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
                }();
                

                但是,以下代码可以编译:

                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.

                另一方面,类似于元组的结构化绑定声明确实引入了变量.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 中捕获这个按引用('&amp;this')? 下一篇:错误:变量“无法隐式捕获,因为未指定默认捕获模式"

                相关文章

                <legend id='WCpy8'><style id='WCpy8'><dir id='WCpy8'><q id='WCpy8'></q></dir></style></legend>

              2. <small id='WCpy8'></small><noframes id='WCpy8'>

              3. <i id='WCpy8'><tr id='WCpy8'><dt id='WCpy8'><q id='WCpy8'><span id='WCpy8'><b id='WCpy8'><form id='WCpy8'><ins id='WCpy8'></ins><ul id='WCpy8'></ul><sub id='WCpy8'></sub></form><legend id='WCpy8'></legend><bdo id='WCpy8'><pre id='WCpy8'><center id='WCpy8'></center></pre></bdo></b><th id='WCpy8'></th></span></q></dt></tr></i><div id='WCpy8'><tfoot id='WCpy8'></tfoot><dl id='WCpy8'><fieldset id='WCpy8'></fieldset></dl></div>
                  • <bdo id='WCpy8'></bdo><ul id='WCpy8'></ul>

                    <tfoot id='WCpy8'></tfoot>