Title
Hiding by lambda captures and parameters
Status
c++17
Section
7.5.6.3 [expr.prim.lambda.capture]
Submitter
Ville Voutilainen

Created on 2015-12-07.00:00:00 last changed 81 months ago

Messages

Date: 2017-02-15.00:00:00

Proposed resolution (February, 2017):

  1. Add the following as a new paragraph after 7.5.6 [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.6 [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
    
Date: 2017-02-15.00:00:00

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

History
Date User Action Args
2018-02-27 00:00:00adminsetmessages: + msg6157
2018-02-27 00:00:00adminsetstatus: open -> c++17
2015-12-07 00:00:00admincreate