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

            Bug ID: 103732
           Summary: Incorrect constexpr evaluation of runtime expression
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gonzalo.gadeschi at gmail dot com
  Target Milestone: ---

GCC 11.2 accepts the following example:

struct S { static constexpr int C = 5; };

void f() {
    S s0{}, s1{};
    S* a[] = {&s0, &s1};

    for (int i = 0; i < 2; ++i) {
        constexpr int x = S::C;
        constexpr int y = s0.C;
        constexpr int z = a[i]->C;
    }
}

According to http://eel.is/c++draft/class.static#general-1, the object
expression of s0.C and a[i]->C are evaluated, but for example, a[i] is not a
constant expression. 

I think GCC is incorrectly accepting this code.

Clang produces the following error:

<source>:10:23: error: constexpr variable 'z' must be initialized by a constant
expression
        constexpr int z = a[i]->C;
                      ^   ~~~~~~~
<source>:10:29: note: read of non-const variable 'i' is not allowed in a
constant expression
        constexpr int z = a[i]->C;
                            ^
<source>:7:14: note: declared here
    for (int i = 0; i < 2; ++i) {
             ^

Reply via email to