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

            Bug ID: 100493
           Summary: Lambda default copy capture that captures "this"
                    cannot be used in both C++17 and C++20 modes
           Product: gcc
           Version: 10.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: a...@cloudius-systems.com
  Target Milestone: ---

Consider the following masterpiece:



struct a {
    int b;
    void f(int x) {
        (void)[=] { (void)(b + x); };
    }
};


The C++20 compiler wants me to disambiguate the [=] capture to mean either [=,
this] or [=, *this]:

<source>:6:15: warning: implicit capture of 'this' via '[=]' is deprecated in
C++20 [-Wdeprecated]
    6 |         (void)[=] { (void)(b + x); };
      |               ^
<source>:6:15: note: add explicit 'this' or '*this' capture

Humoring it, I add ", this" to the capture list:


struct a {
    int b;
    void f(int x) {
        (void)[=, this] { (void)(b + x); };
    }
};


This works in C++20 mode, but in C++17 mode, I get:

source>:6:19: warning: explicit by-copy capture of 'this' redundant with
by-copy capture default
    6 |         (void)[=, this] { (void)(b + x); };
      |                   ^~~~


These warnings are either always on, or enabled by -Wdeprecated. Given I want
-Wdeprecated for other deprecated features, it's impossible for me to write
similar code that works in both C++17 and C++20.

Perhaps the C++20 warning can be moved to a separate flag, to allow library
code that targets both C++17 and C++20.

Reply via email to