https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70774
Bug ID: 70774 Summary: constexpr function with reference parameter gives reinterpret_cast from integer to pointer error Product: gcc Version: 4.9.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: steven.spark at gmail dot com Target Milestone: --- Here is the simplified code: -------------- #define PORTX (*(volatile unsigned char *)(0x1B)) constexpr volatile unsigned char * testRef(volatile unsigned char & x) { return &x; } volatile unsigned char* addr = testRef(PORTX); /// OK constexpr volatile unsigned char* addr2 = testRef(PORTX); /// error: /// reinterpret_cast from integer to pointer -------------- This compiled without problem with gcc 4.8.1, but fails with 4.9.2. Using built-in specs. COLLECT_GCC=avr-g++.exe Target: avr Configured with: /home/toolsbuild/workspace/avr8-gnu-toolchain/src/gcc/configure LDFLAGS=-L/home/toolsbuild/workspace/avr8-gnu-toolchain/avr8-gnu-toolchain-win32_x86-hostlibs/lib CPPFLAGS= --target=avr --host=i686-w64-mingw32 --build=x86_64-pc-linux-gnu --prefix=/home/toolsbuild/workspace/avr8-gnu-toolchain/avr8-gnu-toolchain-win32_x86 --libdir=/home/toolsbuild/workspace/avr8-gnu-toolchain/avr8-gnu-toolchain-win32_x86/lib --enable-languages=c,c++ --with-dwarf2 --enable-doc --disable-shared --disable-libada --disable-libssp --disable-nls --with-avrlibc=yes --with-mpfr=/home/toolsbuild/workspace/avr8-gnu-toolchain/avr8-gnu-toolchain-win32_x86-hostlibs --with-gmp=/home/toolsbuild/workspace/avr8-gnu-toolchain/avr8-gnu-toolchain-win32_x86-hostlibs --with-mpc=/home/toolsbuild/workspace/avr8-gnu-toolchain/avr8-gnu-toolchain-win32_x86-hostlibs --enable-win32-registry=avrtoolchain --with-pkgversion=AVR_8_bit_GNU_Toolchain_3.5.0_1662 --with-bugurl=http://www.atmel.com Thread model: single gcc version 4.9.2 (AVR_8_bit_GNU_Toolchain_3.5.0_1662) I'm writing code for AVR micro controller where every byte counts, so compile time evaluation is key. I was trying out the new Atmel Studio 7 IDE which comes with the new 4.9.2 gcc when I came across this problem. In my real world code I'm using constexpr constructor and constexpr instances of my pin handler class - to make life easier and encapsulate port, data direction and other registers. Note: If I use pointer instead of reference then it compiles fine: constexpr volatile unsigned char * testAddr(volatile unsigned char * x) { return x; } constexpr volatile unsigned char* addr3 = testAddr(&PORTX); /// OK Why is this? Am I doing something wrong? How can I use (port) references in constexpr functions? This also does NOT work: constexpr volatile unsigned char& ref5 = PORTX; /// error