https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116897
Bug ID: 116897
Summary: lambda with deduced this parameter does not capture
this properly
Product: gcc
Version: 14.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: djnz00 at gmail dot com
Target Milestone: ---
Created attachment 59225
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=59225&action=edit
minimized source code
A lambda with a deduced this parameter does not correctly capture this from the
containing scope.
clang compiles this code correctly; gcc incorrectly reports an invalid use of a
non-static data member A::i, despite A *this having been captured.
struct A {
int i = 42;
int foo() {
return [this](this auto &&self) { auto p = &i; return *p; }();
}
};
] clang++ -std=gnu++23 zc9.cc -o zc9
] g++ -std=gnu++23 zc9.cc -o zc9
zc9.cc: In lambda function:
zc9.cc:4:49: error: invalid use of non-static data member ‘A::i’
4 | return [this](this auto &&self) { auto p = &i; return *p; }();
| ^
zc9.cc:2:9: note: declared here
2 | int i = 42;
| ^
zc9.cc: In member function ‘int A::foo()’:
zc9.cc:4:64: error: void value not ignored as it ought to be
4 | return [this](this auto &&self) { auto p = &i; return *p; }();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~