Title
Reference odr-use vs implicit capture
Status
drafting
Section
7.5.5.3 [expr.prim.lambda.capture]
Submitter
Hubert Tong

Created on 2015-02-14.00:00:00 last changed 112 months ago

Messages

Date: 2022-09-25.18:08:42

Whether a reference is odr-used or not has less to do with the context where it is named and more to do with its initializer. In particular, 7.5.5 [expr.prim.lambda] bullet 12.2 leads to cases where references that can never be odr-used are implicitly captured:

A lambda-expression with an associated capture-default that does not explicitly capture this or a variable with automatic storage duration (this excludes any id-expression that has been found to refer to an init-capture's associated non-static data member), is said to implicitly capture the entity (i.e., this or a variable) if the compound-statement:

  • odr-uses (6.3 [basic.def.odr]) the entity, or

  • names the entity in a potentially-evaluated expression (6.3 [basic.def.odr]) where the enclosing full-expression depends on a generic lambda parameter declared within the reaching scope of the lambda-expression.

For example, ref should not be captured in the following:

  struct A {
    A() = default;
    A(const A &) = delete;
  } globalA;

  constexpr bool bar(int &, const A &a) { return &a == &globalA; }

  int main() {
    A &ref = globalA;
    [=](auto q) { static_assert(bar(q, ref), ""); }(0);
  }
History
Date User Action Args
2015-02-14 00:00:00admincreate