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

            Bug ID: 118291
           Summary: Deducing this lambda cannot capture this pointer
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: winmikedows at hotmail dot com
  Target Milestone: ---

The following code is rejected by GCC, but accepted by Clang and MSVC
(https://godbolt.org/z/PeKEbz8od):
```cpp
struct A
{
    int n;
    void fun()
    {
        auto _ = [&](this auto self) { return n; };
    }
};
```

AFAIK, even inside a lambda with an explicit object parameter, "this" pointer
of the enclosing class should still be able to be captured in the normal way.
However, all traditional methods to capture this for this lambda (either the
implicit way like [=] [&] or an explicit [this] or [*this]) will currently be
ignored by GCC, and the access to n will throw an error. Using "return this->n"
in the body also results in the same error complaining about invalid use of
this.
  • [Bug c++/118291] New: Deducing... winmikedows at hotmail dot com via Gcc-bugs

Reply via email to