https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117224
Bug ID: 117224 Summary: warning states "this" is explicit by-copy capture but it's an explicit by-reference capture Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: vries at gcc dot gnu.org Target Milestone: --- Consider test-case: ... $ cat test.cc #include <stdio.h> class X { public: X (int v) { value = v; } X (const X& a) { value = a.value; printf ("copy constructor\n"); } int value; int f (void) { auto l = [=, THIS] { return value + 2; }; return l (); } }; int main (void) { X a (3); return a.f (); } ... As documented [1], a "this" capture means a by-reference capture (so, copy constructor is not called), and a "*this" means a by-copy capture (so, copy constructor is called): ... $ g++ test.cc -DTHIS="this" $ ./a.out $ g++ test.cc -DTHIS="*this" $ ./a.out copy constructor $ ... Sofar the sanity check. With -pedantic, we run into this warning: ... $ g++ test.cc -DTHIS="this" -pedantic test.cc: In member function ‘int X::f()’: <command-line>: warning: explicit by-copy capture of ‘this’ with by-copy capture default only available with ‘-std=c++20’ or ‘-std=gnu++20’ [-Wc++20-extensions] test.cc:18:18: note: in expansion of macro ‘THIS’ 18 | auto l = [=, THIS] { | ^~~~ ... The text of the warning is incorrect: the warning states that "this" is an explicit by-copy capture, but it's an explicit by-reference capture. Clang show this warning text instead: ... $ clang++ test.cc -DTHIS="this" -pedantic test.cc:18:18: warning: explicit capture of 'this' with a capture default of '=' is a C++20 extension [-Wc++20-extensions] 18 | auto l = [=, THIS] { | ^ <command line>:1:14: note: expanded from macro 'THIS' 1 | #define THIS this | ^ 1 warning generated. ... [1] https://en.cppreference.com/w/cpp/language/lambda