cplusplus / draft

C++ standards drafts
http://www.open-std.org/jtc1/sc22/wg21/
5.69k stars 749 forks source link

[dcl.fct.default] Incorrect note on `this` in default arguments #6558

Open Eisenwave opened 1 year ago

Eisenwave commented 1 year ago

[dcl.fct.default] p8 states:

[Note 5: The keyword this cannot appear in a default argument of a member function; see [expr.prim.this].

class A {
  void f(A* p = this) { }           // error
};

This is incorrect, because the following program is well-formed and the keyword this appears in a default argument:

class A {
  void f(auto = [] {
    struct B { B() { this; } }; // keyword this appeared in a default argument
  }) { }
};

Syntactically, the entire initializer-clause is the default argument, and the this keyword obviously appears inside of it. See [dcl.fct.default] p1

Solution

We could say "cannot directly appear" but this is wishy washy, though correct according to some reader's imaginations.

Wording it in terms of "current class" and whatnot is quite difficult, so maybe we could just say:

Except within the block scope of a lambda-expression, the this keyword cannot ...

It's just a note, so we don't need to make it cover all the cases in all the detail; we just need to make it correct.

jensmaurer commented 1 year ago

Stuff in the body of a lambda is not a subexpression of the lambda. Maybe we can just say that this cannot appear as a subexpression of a default argument of a member function?