Title
Lambdas in default arguments vs the ODR
Status
nad
Section
6.3 [basic.def.odr]
Submitter
Richard Smith

Created on 2017-11-10.00:00:00 last changed 38 months ago

Messages

Date: 2021-02-15.00:00:00

Rationale (February, 2021):

The resolution of issue 2300 makes clear that this example is an ODR violation.

Date: 2018-10-15.00:00:00

Notes from the October, 2018 teleconference:

This will be addressed by work already underway to rework the relationship between lambdas and the ODR.
Date: 2022-09-25.18:08:42

According to 6.3 [basic.def.odr] bullet 6.5,

  • in each definition of D, a default argument used by an (implicit or explicit) function call is treated as if its token sequence were present in the definition of D; that is, the default argument is subject to the requirements described in this paragraph (and, if the default argument has subexpressions with default arguments, this requirement applies recursively)

However, this rule is insufficient to handle a case like:

  struct A {
    template<typename T> A(T);
  };
  void f(A a = []{});
  inline void g() {
    f();
  }

This should be an ODR violation, because the call to f() will invoke a different specialization of the constructor template in each translation unit, but it is not, because the rule says this example is equivalent to:

  inline void g() {
   f([]{});
  }

which is not an ODR violation, since the type of the closure object will be the same in every translation unit (9.2.8 [dcl.inline] paragraph 6) ..

History
Date User Action Args
2021-02-17 00:00:00adminsetmessages: + msg6500
2021-02-17 00:00:00adminsetstatus: open -> nad
2020-12-15 00:00:00adminsetmessages: + msg6257
2017-11-10 00:00:00admincreate