public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100493] New: Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes
@ 2021-05-09 15:19 avi@cloudius-systems.com
  2021-07-21  9:26 ` [Bug c++/100493] " gcc at bmevers dot de
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: avi@cloudius-systems.com @ 2021-05-09 15:19 UTC (permalink / raw)
  To: gcc-bugs

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: avi@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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-12-15 19:58 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-09 15:19 [Bug c++/100493] New: Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes avi@cloudius-systems.com
2021-07-21  9:26 ` [Bug c++/100493] " gcc at bmevers dot de
2021-07-21 15:36 ` redi at gcc dot gnu.org
2021-09-22 14:13 ` redbeard0531 at gmail dot com
2021-09-22 14:30 ` redbeard0531 at gmail dot com
2021-11-29 12:55 ` cvs-commit at gcc dot gnu.org
2021-11-29 13:01 ` ppalka at gcc dot gnu.org
2021-12-15 19:55 ` cvs-commit at gcc dot gnu.org
2021-12-15 19:58 ` ppalka at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).