<bdo id='Zyx7G'></bdo><ul id='Zyx7G'></ul>
<tfoot id='Zyx7G'></tfoot>

  • <small id='Zyx7G'></small><noframes id='Zyx7G'>

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

        Lambda 捕获和具有相同名称的参数 - 谁在影响另一个?(clang vs gcc)

        Lambda capture and parameter with same name - who shadows the other? (clang vs gcc)(Lambda 捕获和具有相同名称的参数 - 谁在影响另一个?(clang vs gcc))

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

                • <bdo id='JXrWY'></bdo><ul id='JXrWY'></ul>

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

                  本文介绍了Lambda 捕获和具有相同名称的参数 - 谁在影响另一个?(clang vs gcc)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  auto foo = "你正在使用 g++!";auto compiler_detector = [foo](auto foo) { std::puts(foo);};compiler_detector("你正在使用clang++!");

                  • clang++ 3.6.0 和更新版本打印出您正在使用 clang++!" 并警告 捕获 foo 未使用.

                  • g++ 4.9.0 和更新版本打印出您正在使用 g++!" 并警告 参数 foo 未使用.

                  这里哪个编译器更准确地遵循 C++ 标准?

                  魔杖盒示例

                  解决方案

                  更新:正如核心主席在底部引用中所承诺的,代码为 现在格式错误:

                  <块引用>

                  如果simple-capture中的标识符作为lambda-declarator<的参数的declarator-id出现/em> 的参数声明子句,程序格式错误.

                  <小时>

                  不久前在 lambdas 中存在一些关于名称查找的问题.它们由 N2927 解决:

                  <块引用>

                  新措辞不再依赖于查找来重新映射捕获实体的使用.更清楚否认 lambda 的 复合语句 分两次处理,或者该 复合语句 中的任何名称都可能解析为闭包类型的成员.

                  查找总是在 lambda 表达式 的上下文中完成,永远不会在"转换为闭包类型的成员函数体之后.见[expr.prim.lambda]/8::><块引用>

                  lambda 表达式复合语句产生函数的函数体 ([dcl.fct.def])调用运算符,但出于名称查找的目的 [...],复合语句 被视为在 lambda 表达式 的上下文中.[ 示例:

                  struct S1 {整数 x, y;int 运算符()(int);无效 f() {[=]()->int {return operator()(this->x+y);//等价于:S1::operator()(this->x+(*this).y)//这有类型 S1*};}};

                  结束示例 ]

                  (该示例还清楚地表明,查找不会以某种方式考虑生成的闭包类型的捕获成员.)

                  名称 foo 未在捕获中(重新)声明;它在包含 lambda 表达式的块中声明.参数 foo 在嵌套在该外部块中的块中声明(参见 [basic.scope.block]/2,其中也明确提到了 lambda 参数).查找的顺序显然是从内到外块.因此应该选择参数,即Clang是对的.

                  如果您要使捕获成为初始化捕获,即 foo = "" 而不是 foo,答案就不清楚了.这是因为现在捕获实际上诱导声明,其阻止"没有给出.我给核心主席发了信息,他回复了

                  <块引用>

                  这是第 2211 期(很快就会在 open-std.org 网站上出现一个新的问题列表,不幸的是只有一些问题的占位符,这是其中一个;我正在努力填补这些空白在月底的科纳会议之前).CWG 在我们 1 月的电话会议上讨论了这个问题,如果捕获名称也是参数名称,则方向是使程序格式错误.

                  auto foo = "You're using g++!";
                  auto compiler_detector = [foo](auto foo) { std::puts(foo); };
                  compiler_detector("You're using clang++!");
                  

                  • clang++ 3.6.0 and newer print out "You're using clang++!" and warn about the capture foo being unused.

                  • g++ 4.9.0 and newer print out "You're using g++!" and warn about the parameter foo being unused.

                  What compiler is more accurately following the C++ Standard here?

                  wandbox example

                  解决方案

                  Update: as promised by the Core chair in the bottom quote, the code is now ill-formed:

                  If an identifier in a simple-capture appears as the declarator-id of a parameter of the lambda-declarator's parameter-declaration-clause, the program is ill-formed.


                  There were a few issues concerning name lookup in lambdas a while ago. They were resolved by N2927:

                  The new wording no longer relies on lookup to remap uses of captured entities. It more clearly denies the interpretations that a lambda's compound-statement is processed in two passes or that any names in that compound-statement might resolve to a member of the closure type.

                  Lookup is always done in the context of the lambda-expression, never "after" the transformation to a closure type's member function body. See [expr.prim.lambda]/8:

                  The lambda-expression's compound-statement yields the function-body ([dcl.fct.def]) of the function call operator, but for purposes of name lookup, […], the compound-statement is considered in the context of the lambda-expression. [ Example:

                  struct S1 {
                    int x, y;
                    int operator()(int);
                    void f() {
                      [=]()->int {
                        return operator()(this->x+y);  // equivalent to: S1::operator()(this->x+(*this).y)
                                                       // and this has type S1*
                      }; 
                    }
                  };
                  

                  end example ]

                  (The example also makes clear that lookup does not somehow consider the generated capture member of the closure type.)

                  The name foo is not (re)declared in the capture; it is declared in the block enclosing the lambda expression. The parameter foo is declared in a block that is nested in that outer block (see [basic.scope.block]/2, which also explicitly mentions lambda parameters). The order of lookup is clearly from inner to outer blocks. Hence the parameter should be selected, that is, Clang is right.

                  If you were to make the capture an init-capture, i.e. foo = "" instead of foo, the answer would not be clear. This is because the capture now actually induces a declaration whose "block" is not given. I messaged the core chair on this, who replied

                  This is issue 2211 (a new issues list will appear on the open-std.org site shortly, unfortunately with just placeholders for a number of issues, of which this is one; I'm working hard to fill in those gaps before the Kona meeting at the end of the month). CWG discussed this during our January teleconference, and the direction is to make the program ill-formed if a capture name is also a parameter name.

                  这篇关于Lambda 捕获和具有相同名称的参数 - 谁在影响另一个?(clang vs gcc)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)
                • <tfoot id='ykuOb'></tfoot>
                  <i id='ykuOb'><tr id='ykuOb'><dt id='ykuOb'><q id='ykuOb'><span id='ykuOb'><b id='ykuOb'><form id='ykuOb'><ins id='ykuOb'></ins><ul id='ykuOb'></ul><sub id='ykuOb'></sub></form><legend id='ykuOb'></legend><bdo id='ykuOb'><pre id='ykuOb'><center id='ykuOb'></center></pre></bdo></b><th id='ykuOb'></th></span></q></dt></tr></i><div id='ykuOb'><tfoot id='ykuOb'></tfoot><dl id='ykuOb'><fieldset id='ykuOb'></fieldset></dl></div>

                  • <bdo id='ykuOb'></bdo><ul id='ykuOb'></ul>
                        <tbody id='ykuOb'></tbody>

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

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