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

            Bug ID: 90852
           Summary: A recurring bug: Constant expressions support
                    reinterpret_cast
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zhonghao at pku dot org.cn
  Target Milestone: ---

A previous bug report (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49171)
contains two samples:

//---
constexpr const char* c = reinterpret_cast<const char*>(0x123);
//---


//----------------
struct X {
 X* operator&();
};

X x[2];

const bool p = (reinterpret_cast<X*>(&reinterpret_cast<char&>(x[1]))
- reinterpret_cast<X*>(&reinterpret_cast<char&>(x[0]))) == sizeof(X);

enum E { e = p }; // e should have a value equal to 1
//----------------


After some discussions, programmers agree that the first sample shall be
reject, but the second sample has its value. gcc 7.2 accepts the code. However,
gcc 10.0.0 rejects it:

source>:10:14: error: the value of 'p' is not usable in a constant expression

   10 | enum E { e = p }; // e should have a value equal to 1

      |              ^

<source>:7:12: note: 'p' was not initialized with a constant expression

    7 | const bool p = (reinterpret_cast<X*>(&reinterpret_cast<char&>(x[1]))

      |            ^

<source>:10:14: error: the value of 'p' is not usable in a constant expression

   10 | enum E { e = p }; // e should have a value equal to 1

      |              ^

<source>:7:12: note: 'p' was not initialized with a constant expression

    7 | const bool p = (reinterpret_cast<X*>(&reinterpret_cast<char&>(x[1]))

      |            ^

<source>:10:14: error: enumerator value for 'e' is not an integer constant

   10 | enum E { e = p }; // e should have a value equal to 1

      |              ^

Compiler returned: 1

Reply via email to