https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77564
Bug ID: 77564 Summary: Code fails to link when compiled with -O0 Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: csaba_22 at yahoo dot co.uk Target Milestone: --- The following code fails to link when compiled with -O0. It links when compiled with -O1 or higher, or -Og: template <typename R> class du { R r; public: constexpr du() = default; du(const du&) = default; template<typename R2> constexpr explicit du(const R2& rep) : r(static_cast<R>(rep)) { } }; typedef du<int> secs; class DWP { secs to; public: DWP(secs x); }; DWP::DWP(secs x) : to(x) {} class FHT { public: FHT() : dwp{secs(READ_TIMEOUT)} {} protected: static auto constexpr READ_TIMEOUT = 1; DWP dwp; }; int main() { FHT fht; } $ g++-620 -Wall -pedantic -Wextra -std=c++11 constexpr.cc -o constexpr /tmp/ccoUUTbu.o: In function `FHT::FHT()': constexpr.cc:(.text._ZN3FHTC2Ev[_ZN3FHTC5Ev]+0x11): undefined reference to `FHT::READ_TIMEOUT' $ nm constexpr.o | c++filt 0000000000000017 T main 0000000000000000 W du<int>::du<int>(int const&) 0000000000000000 T DWP::DWP(du<int>) 0000000000000000 T DWP::DWP(du<int>) U FHT::READ_TIMEOUT 0000000000000000 W FHT::FHT() 0000000000000000 W FHT::FHT() 0000000000000000 n FHT::FHT() If compiling with -O1: $ nm constexpr.o | c++filt 0000000000000003 T main 0000000000000000 T DWP::DWP(du<int>) 0000000000000000 T DWP::DWP(du<int>) Seen with a hand-built GCC 6.2: $ g++-620 -v Using built-in specs. COLLECT_GCC=g++-620 COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/6.2.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../gcc-6.2.0/configure --program-suffix=-620 --enable-version-specific-runtime-libs --enable-languages=c,c++,lto Thread model: posix gcc version 6.2.0 (GCC) This has been seen with GCC as far back as gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)