Lambdas in default arguments vs the ODR
6.3 [basic.def.odr]
Richard Smith

Created on 2017-11-10.00:00:00 last changed 1 month ago


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: 2017-11-10.00:00:00

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() {

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() {

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

Date User Action Args
2020-12-15 00:00:00adminsetmessages: + msg6257
2017-11-10 00:00:00admincreate