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

            Bug ID: 89576
           Summary: constexpr not working if implicitly captured in a
                    lambda in a function template (gcc 8.3+)
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wielkiegie at gmail dot com
  Target Milestone: ---

There are many similar bugs that I have found in the bugzilla (see below), but
this one differs from the other ones:
1. It happens only in gcc 8.3 and trunk (gcc 8.2 and earlier are fine)
2. The lambda is not generic (but it is contained in a function template which
might be similar internally)
3. The capture is implicit

Example to reproduce:

------------------------------------------------
template <class T>
void foo()
{
    constexpr int x = 0;
    [&] {
        if constexpr (x) {}
    };
}
------------------------------------------------

Produces the following in gcc 8.3 and trunk:

------------------------------------------------
<source>: In lambda function:

<source>:6:24: error: lambda capture of 'x' is not a constant expression

         if constexpr (x) {}

                        ^
------------------------------------------------

The following changes to the example make it compile:
1. Adding static to the constexpr int
2. Making it a regular function and not a template
3. Removing the [&] capture

Changing the capture from [&] to [=] doesn't help.

If I change it to an explicit capture [x], it fails in all gcc versions except
gcc 7.4 and passes on clang.

The other similar bug reports: bug 86429, bug 82643

Reply via email to