0

The following code fails to compile with GCC 6 and 32bit-mode (on a amd64 host).

class foo {
  static constexpr const char* ptr = reinterpret_cast<const char*>(0xff);
};

int main() {
  return 0;
}

Compilation command:

g++ -m32 test.cc

Error message is:

test.cc:2:38: error: reinterpret_cast from integer to pointer
   static constexpr const char* ptr = reinterpret_cast<const char*>(0xff);
                                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It works with older compilers (GCC 4.9). It also works as inline definition (not as static member variable) or compiled in 64 bit mode.

Is there a way to get it working with GCC 6?

gerion
  • 345
  • 2
  • 9
  • Can you alter the constant to add the size suffix? Maybe that'd help. – Bartek Banachewicz Apr 26 '17 at 16:17
  • Related to [getting-around-the-reinterpret-cast-limitation-with-constexpr](http://stackoverflow.com/questions/26200827/getting-around-the-reinterpret-cast-limitation-with-constexpr) – Jarod42 Apr 26 '17 at 16:18
  • Why do you think you need `reinterpret_cast` rather than `static_cast`? – Toby Speight Apr 26 '17 at 16:19
  • @Jarod42 And GCC enforces this only in 32 bit mode? – gerion Apr 26 '17 at 16:32
  • @gerion: it should also be invalid in 64bit mode... Maybe the check is done according to (possible) `sizeof(int)` and `sizeof(void*)` and so is not caught in 64bits mode.. – Jarod42 Apr 26 '17 at 16:41

0 Answers0