Skip to content

[expr.prim.this] Clarify that this can appear within a lambda with an explicit object parameter #6855

@zygoloid

Description

@zygoloid

[expr.prim.this]/3 says:

[this] shall not appear within the declaration of either a static member function or an explicit object member function of the current class (although its type and value category are defined within such member functions as they are within an implicit object member function).

Multiple implementations have misimplemented this rule for the case of this appearing in a lambda that has an explicit object parameter:

class C {
  void f() {
    [this] (this auto self) { return this->n; };
  }
  int n;
}

Here, I think the three uses of this are not within the declaration of an explicit object member function, because a lambda-expression is not a function declaration at all. And this example should be valid -- there's no problem with using this in a lambda with an explicit object parameter, because the this refers to the object parameter of the enclosing function declaration, not the lambda-expression.

Perhaps it'd be useful to add a note to this parameter to remind the reader that a lambda-expression is not a declaration of a function, so you need to keep looking outwards to find the function you're looking for.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions