This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 108i. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2022-07-16


2211. Hiding by lambda captures and parameters

Section: 7.5.5.3  [expr.prim.lambda.capture]     Status: C++17     Submitter: Ville Voutilainen     Date: 2015-12-07

[Adopted at the February/March, 2017 meeting.]

Consider:

  #include <iostream>

  int main()
  {
    [x=2](int x) { std::cout << x << std::endl; }(3);
  }

What is the code supposed to print? There is implementation divergence.

Proposed resolution (February, 2017):

  1. Add the following as a new paragraph after 7.5.5 [expr.prim.lambda] paragraph 11:

  2. The identifier in a simple-capture is looked up using the usual rules for unqualified name lookup (6.5.3 [basic.lookup.unqual]); each such lookup shall find an entity. An entity that is designated by a simple-capture is said to be explicitly captured, and shall be *this (when the simple-capture is “this ” or “* this ”) or a variable with automatic storage duration declared in the reaching scope of the local lambda expression.

    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. [Example:

      void f() {
        int x = 0;
        auto g = [x](int x) { return 0; }  // error: parameter and simple-capture have the same name
      }
    

    end example]

  3. Change the example of 7.5.5 [expr.prim.lambda] paragraph 12 as follows:

  4.   int x = 4;
      auto y = [&r = x, x = x+1]()->int {
                      r += 2;
                      return x+2;
                   }(); // Updates ::x to 6, and initializes y to 7.
      auto z = [a = 42](int a) { return 1; } // error: parameter and local variable have the same name