https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63715
Bug ID: 63715
Summary: Compiler giving "reinterpret_cast from integer to
pointer" in constexpr function
Product: gcc
Version: 4.9.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: mbenda at omsquare dot com
Created attachment 33865
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=33865&action=edit
Code snippet giving the error
Hi!
I have a code that involves casting from integers to pointers and back
(barebone I/O register access on ARM) and to achieve optimal machine code I use
constexpr on such expressions. Everything compiles fine with gcc-4.8.3, the
resulting code works. But after upgrading to gcc-4.9.2 I get several
"reinterpret_cast from integer to pointer" compilation errors. The gcc used is
compiled with --target=arm-none-eabi.
A simplified code snippet that manifests this error (also attached):
-----------
typedef unsigned int u32;
struct IO { u32 reg; };
constexpr IO* io = (IO*) 0x40000000;
inline constexpr u32 addr(u32& reg) {
return reinterpret_cast<u32>(®);
}
struct IOTraits {
static constexpr u32 addr1 = reinterpret_cast<u32>(&io->reg); // works fine
static constexpr u32 addr2 = addr(io->reg); // compilation error
};
-----------
Both IOTraits::addr1 and addr2 are initialized with the same expression. The
only difference is that in the latter case, part of the expression is
encapsulated in an inline constexpr function (addr). The line with addr2 gives:
gccbug.cc:15:43: error: reinterpret_cast from integer to pointer
But the result of the expression is well-defined in compile time... Moreover,
gcc-4.8.3 compiles this code just fine.
The following bugs are probably related to this problem: #56728 and #49171.