https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100710
Bug ID: 100710 Summary: static_cast to derived* of base* pointing to non-static data member of base type not rejected in constant expression Product: gcc Version: 11.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: jason.e.cobb at gmail dot com Target Milestone: --- In the following code: [code] class B {}; class D : public B { public: B member; }; constexpr bool test(bool do_ub = true) { if (do_ub) { D d; return static_cast<D*>(&d.member) != nullptr; } return false; } static_assert(test()); [/code] On Compiler Explorer: https://godbolt.org/z/eKEcT7bhb GCC 11.1 accepts when it should reject due to the static_cast being UB during the evaluation of a constant expression, as the pointer to B does not actually point to a base class subobject of an object of type D. >From [expr.static.cast]/11: > If the prvalue of type “pointer to cv1 B” points to a B that is actually a > subobject of an object of type D, the resulting pointer points to the > enclosing object of type D. Otherwise, the behavior is undefined. This wording is slightly broken because it says "actually a subobject" instead of "actually a base class subobject", but there is an editorial PR to fix this: https://github.com/cplusplus/draft/pull/4605