https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77598
Bug ID: 77598 Summary: consexpr compilation failure on reference type casting Product: gcc Version: 4.8.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: bobk-off at yandex dot ru Target Milestone: --- Compiler fails on type casting of reference during constexpr processing. This bug is actual for GCC-ARM 4.8.2+ compilers, and GCC-X86-64 5.3+ compilers (tested on gcc.godbolt.org). clang of any version which supports C++14 compile code below successfully. Known walkaround: change type of dtarr variable to "DataTypeBasicX_t &" #include <cstdint> #include <array> template<typename T> struct DefaultInstanceOf { static constexpr T value = T(); }; template<typename T> constexpr T DefaultInstanceOf<T>::value; enum class BasicDataTypeId_t : uint16_t { None = 0, INTEGER16 = 0x0003, }; struct DataTypeX_t { private: uint32_t mBitSize; public: constexpr uint32_t BitSize() const { return mBitSize; } constexpr DataTypeX_t(const uint32_t & bitSize) : mBitSize(bitSize) {}; }; struct DataTypeBasicX_t : public DataTypeX_t { constexpr DataTypeBasicX_t() : DataTypeX_t(50) {}; }; struct DataTypeBasicArrayX_t : public DataTypeBasicX_t { }; struct BaseDataTypes { static constexpr const auto & ARRAY_OF_INT = DefaultInstanceOf<DataTypeBasicArrayX_t>::value; }; constexpr const DataTypeX_t & dtarr10 = BaseDataTypes::ARRAY_OF_INT; constexpr const size_t bitsz10 = dtarr10.BitSize(); int main() { while (1); } Error message: 46 : in constexpr expansion of '(& dtarr10)->DataTypeX_t::BitSize()' 46 : error: accessing value of 'DefaultInstanceOf<DataTypeBasicArrayX_t>::value' through a 'const DataTypeX_t' glvalue in a constant expression constexpr const size_t bitsz10 = dtarr10.BitSize(); ^ Compilation failed Link to gcc.godbolt.org code: https://godbolt.org/g/lR0dE4