https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107622
--- Comment #4 from Wilhelm M <klaus.doldinger64 at googlemail dot com> --- In the following class the static data member is accessible via explicit template instantiation from the outside. So the compiler cannot reason that the value is in [0,2]. But this does not hold for the function g(): here mState2 is definitely not accessible from outside. But here the std::unreachable() is still neccessary. here the optimization is missing. struct FSM { enum class State : uint8_t {A, B, C}; static void f() { switch(mState) { case State::A: o = 10; break; case State::B: o = 11; break; case State::C: o = 12; break; default: // std::unreachable(); break; } } static void g() { static State mState2{State::A}; // not accessible from outside switch(mState2) { case State::A: o = 10; break; case State::B: o = 11; break; case State::C: o = 12; break; default: // std::unreachable(); break; } } private: inline static State mState{State::A}; // still modifyable via explicit template instantiation };