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


2745. Dependent odr-use in generic lambdas

Section: 6.3  [basic.def.odr]     Status: DRWP     Submitter: Shafik Yaghmour     Date: 2022-12-13

[Accepted as a DR at the March, 2024 meeting.]

Default template arguments of generic lambdas can refer to local variables. It is unclear whether the potential odr-use is checked when parsing the template definition or when instantiating the template.

There is wide implementation divergence.

Proposed resolution (approved by CWG 2024-03-20):

Insert a new paragraph before 6.3 [basic.def.odr] paragraph 11:

[ Example:

  void g() {
    constexpr int x = 1;
    auto lambda = [] <typename T, int = ((T)x, 0)> {};  // OK
    lambda.operator()<int, 1>();         // OK, does not consider x at all
    lambda.operator()<int>();            // OK, does not odr-use x
    lambda.operator()<const int&>();     // error: odr-uses x from a context where x is not odr-usable
  }

  void h() {
    constexpr int x = 1;
    auto lambda = [] <typename T> { (T)x; };  // OK
    lambda.operator()<int>();            // OK, does not odr-use x
    lambda.operator()<void>();           // OK, does not odr-use x
    lambda.operator()<const int&>();     // error: odr-uses x from a context where x is not odr-usable
  }

-- end example ]

Every program shall contain at least one definition of every function or variable ...