cplusplus / cplusplus/draft

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

Open
#6,855 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

P3-Other
Dominant language
TeX
Stars
221
Forks
813
Avg merge
16h 4m
Merged PRs (30d)
36

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at [expr.prim.this]/3 and review the supplied lambda example alongside the rule for explicit object member functions. Done when the draft clearly explains whether this in a lambda with an explicit object parameter is governed by the enclosing function and avoids the reported implementation misreading.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.