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

2024-10-26


2358. Explicit capture of value

Section: 7.5.6.3  [expr.prim.lambda.capture]     Status: CD5     Submitter: Daveed Vandevoorde     Date: 2017-09-20

[Accepted as a DR at the February, 2019 meeting.]

The status of an example like the following is unclear:

  int foo(int a = ([loc=1] { return loc; })()) { return a; }

because of 7.5.6.3 [expr.prim.lambda.capture] paragraph 9:

A lambda-expression appearing in a default argument shall not implicitly or explicitly capture any entity.

However, there doesn't appear to be a good reason for prohibiting such a capture.

Notes from the April, 2018 teleconference:

CWG felt that the rule for capturing should be something like the prohibition for local classes odr-using a variable with automatic storage duration in 11.6 [class.local] paragraph 1.

Proposed resolution (November, 2018):

Change 7.5.6.3 [expr.prim.lambda.capture] paragraph 9 as follows:

A lambda-expression appearing in a default argument shall not implicitly or explicitly capture any entity, except for an init-capture for which any full-expression in its initializer satisfies the constraints of an expression appearing in a default argument (9.3.4.7 [dcl.fct.default]). [Example:

  void f2() {
    int i = 1;
    void g1(int = ([i]{ return i; })());       // ill-formed
    void g2(int = ([i]{ return 0; })());       // ill-formed
    void g3(int = ([=]{ return i; })());       // ill-formed
    void g4(int = ([=]{ return 0; })());       // OK
    void g5(int = ([]{ return sizeof i; })()); // OK
    void g6(int = ([x=1] { return x; }))();    // OK
    void g7(int = ([x=i] { return x; }))();    // ill-formed
  }

end example]