public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95269] New: Lambda is allowed to capture any constexpr variable without specifying any captures
@ 2020-05-22  8:30 dragondreamer at live dot com
  2021-08-28 22:34 ` [Bug c++/95269] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: dragondreamer at live dot com @ 2020-05-22  8:30 UTC (permalink / raw)
  To: gcc-bugs

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."

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

end of thread, other threads:[~2022-02-03  5:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-22  8:30 [Bug c++/95269] New: Lambda is allowed to capture any constexpr variable without specifying any captures dragondreamer at live dot com
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

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).