https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116727

            Bug ID: 116727
           Summary: "this" is unusable in an explicit object member
                    function lambda capturing this
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: janschultke at googlemail dot com
  Target Milestone: ---

struct awoo {
    void chan() {
        [this](this auto&& self) {
            this;
        };
    }
};

Clang correctly accepts this, GCC rejects it with the error
(https://godbolt.org/z/rKbzxn863):

> <source>:4:13: error: invalid use of 'this' in non-member function 
> [-Wtemplate-body]
>     4 |             this;
>       |             ^~~~


It seems like GCC is falsely rejecting the use of "this" here because the
lambda's call operator is an explicit object member function.

However, lambda bodies do not introduce a class scope (see
http://eel.is/c++draft/expr.prim.this#note-1) and
http://eel.is/c++draft/expr.prim.this#3.sentence-2 states:
> It shall not appear within the declaration of a static or explicit object 
> member function of the current class ...

Since there is no current class, it is legal to use "this" in principle, and
the rules for what "this" does should be those stated in [expr.prim.lambda], as
usual.
  • [Bug c++/116727] New: "... janschultke at googlemail dot com via Gcc-bugs

Reply via email to