public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "dragondreamer at live dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/95269] New: Lambda is allowed to capture any constexpr variable without specifying any captures
Date: Fri, 22 May 2020 08:30:38 +0000	[thread overview]
Message-ID: <bug-95269-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 95269
           Summary: Lambda is allowed to capture any constexpr variable
                    without specifying any captures
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dragondreamer at live dot com
  Target Milestone: ---

G++ (at least these versions: 8.2.1, 9.2.1, 10.1.0, trunk) successfully
compiles the following code:

// ===================================
template<typename Lambda>
constexpr void test(Lambda lambda)
{
    constexpr auto result = lambda();
    static_assert(result.value == 1);
}

struct teststruct
{
    int value = 0;
};

constexpr auto get_value()
{
    teststruct result{};
    result.value = 1;
    return result;
}

int main()
{
    constexpr auto value = get_value();
    //Here's a lambda without any captures
    auto options_lambda = []() { return value; };
    test(options_lambda);
}
// ===================================

This code instead should produce an error when trying to create a lambda which
uses "value" without capturing it. This is the exact behavior of Clang and
MSVC, which both produce an error message.

As far as I understand, the following rule must be applied here:
"If a lambda expression (or an instantiation of a generic lambda's function
call operator) ODR-uses this or any variable with automatic storage duration,
it must be captured by the lambda expression."

             reply	other threads:[~2020-05-22  8:30 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-22  8:30 dragondreamer at live dot com [this message]
2021-08-28 22:34 ` [Bug c++/95269] " pinskia at gcc dot gnu.org
2021-08-28 22:42 ` pinskia at gcc dot gnu.org
2022-02-03  5:00 ` 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-95269-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).