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

            Bug ID: 120123
           Summary: Implicit this is not used in a requires clause in
                    nested lambdas
           Product: gcc
           Version: 15.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: valentin at tolmer dot fr
  Target Milestone: ---

Minimal example: https://godbolt.org/z/GxW6hjj5e

```
struct H {
    void member(int) {}
    void call() {
        [this]() {
            [this](const auto& v)
                requires requires { /*this->*/member(v); }
            { return member(v); }(0);
        };
    }
};
```
$ g++ -std=c++23 -fconcepts-diagnostics-depth=10
<source>: In lambda function:
<source>:7:34: error: no match for call to
'(H::call()::<lambda()>::<lambda(const auto:1&)>) (int)'
    5 |             [this](const auto& v)
      |             ~~~~~~~~~~~~~~~~~~~~~ 
    6 |                 requires requires { /*this->*/member(v); }
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    7 |             { return member(v); }(0);
      |             ~~~~~~~~~~~~~~~~~~~~~^~~
<source>:5:18: note: there is 1 candidate
    5 |             [this](const auto& v)
      |                  ^
<source>:5:13: note: candidate 1: 'template<class auto:1>
H::call()::<lambda()>::<lambda(const auto:1&)>'
    5 |             [this](const auto& v)
      |             ^
<source>:5:13: note: template argument deduction/substitution failed:
<source>:5:13: note: constraints not satisfied
<source>: In substitution of 'template<class auto:1>
H::call()::<lambda()>::<lambda(const auto:1&)> [with auto:1 = int]':
<source>:7:34:   required from here
    5 |             [this](const auto& v)
      |             ~~~~~~~~~~~~~~~~~~~~~ 
    6 |                 requires requires { /*this->*/member(v); }
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    7 |             { return member(v); }(0);
      |             ~~~~~~~~~~~~~~~~~~~~~^~~
<source>:5:13:   required by the constraints of 'template<class auto:1>
H::call()::<lambda()>::<lambda(const auto:1&)>'
<source>:6:26:   in requirements  [with auto:1 = int]
<source>:6:53: note: the required expression 'H::member(v)' is invalid, because
    6 |                 requires requires { /*this->*/member(v); }
      |                                               ~~~~~~^~~
<source>:6:53: error: cannot call member function 'void H::member(int)' without
object
Compiler returned: 1

Uncommenting the /*this->*/ in the requires clause makes the code compile.
Removing the outer lambda makes the code compile. Clang accepts the code.

>From cppreference, "Requirements may refer to the template parameters that are
in scope, to the parameters of parameter-list, and to any other declarations
that are visible from the enclosing context." It doesn't specify whether "this"
should be implicitly used or not, but it is at least surprising and the
behavior differs from the body.

Reply via email to