public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "ozixtheorange at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/106508] New: Extra warnings with lambda captures
Date: Tue, 02 Aug 2022 18:20:48 +0000	[thread overview]
Message-ID: <bug-106508-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 106508
           Summary: Extra warnings with lambda captures
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ozixtheorange at gmail dot com
  Target Milestone: ---

The following will not work, because `this` needs to be captured to access
`member`. Seems like GCC should be able to detect that `member` is a class
member and warn that `this` is not being captured:

int outer;
struct A{
  int member = 0;
  auto fn(){
     return [&]{
       outer = member; // invalid
     };
  }
};

The following also fails, this time because `val` goes out of scope and so the
reference to `val` is invalid. I don't know if it is feasible for GCC to detect
this scenario, e.g. the lambda's lifetime extends beyond the scope of
captured-by-reference variables; but, a warning in this case would be nice as
well.

int outer;
auto fn1(int val = 0){
  return [&]{
    outer = val; // invalid
  };
}
std::thread lambda;
void fn2(int val = 0){
  lambda = std::thread([&]{
    outer = val; // invalid
  });
}

The reason I suggest this is because the compilation will succeed without
warning, but the binary can segfault and it is difficult to trace where the
source of the problem is. The first case is more subtle (forgetting `this`),
the second perhaps a bit easier to recognize though when debugging.

             reply	other threads:[~2022-08-02 18:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-02 18:20 ozixtheorange at gmail dot com [this message]
2022-12-11  1:51 ` [Bug c++/106508] " pinskia at gcc dot gnu.org
2022-12-11  1:55 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-106508-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).