https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87634
Bug ID: 87634 Summary: CSE for dynamic_cast Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: Simon.Richter at hogyros dot de Target Milestone: --- With the code struct A { virtual void foo() = 0; }; struct B : A { virtual void foo() {} void bar() const; }; void test(A *a) { if(auto b = dynamic_cast<B *>(a)) b->bar(); if(auto b = dynamic_cast<B *>(a)) b->bar(); } I'd expect the type of the object to be unchanged between the two `dynamic_cast` invocations, so the second type check would be unnecessary. The generated code does two checks, however. It is in theory possible to replace the object in-place with one of different type if it is also accessible through a global pointer from within `B::bar()`, but is this a good enough reason to repeat the type check, or would it be possible to optimize out the second dynamic_cast<> here?