CWG Issue 2211 (original) (raw)

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

2025-04-13


2211. Hiding by lambda captures and parameters

Section: 7.5.6.3 [expr.prim.lambda.capture]Status: C++17Submitter: Ville VoutilainenDate: 2015-12-07

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

Consider:

#include

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.6 [expr.prim.lambda] paragraph 11:

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_]

  1. Change the example of 7.5.6 [expr.prim.lambda] paragraph 12 as follows:

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