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

* [Bug c++/95269] Lambda is allowed to capture any constexpr variable without specifying any captures
  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 ` 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
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-28 22:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
GCC and ICC accept this while clang and MSVC reject this.

Without constexpr for value, GCC and ICC do reject this.

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

* [Bug c++/95269] Lambda is allowed to capture any constexpr variable without specifying any captures
  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
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-28 22:42 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=93842

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So I don't think this is an ODR use of the constexpr.
If I change the lambda be this, GCC rejects it correctly.

    auto options_lambda = []() ->auto& { return value; };


So it all depends on what the return type of the original lambda should be
really.

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

* [Bug c++/95269] Lambda is allowed to capture any constexpr variable without specifying any captures
  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
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-03  5:00 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 88164.

*** This bug has been marked as a duplicate of bug 88164 ***

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