Even under -O3, the assembly generated for foo1 and foo2 is vastly inferior to the one generated for foo3, even though code-wise all three are identical.
class Base1 { int data; }; class Base2 { int data; }; class Derived : public Base1, public Base2 { public: int data; }; int foo1(Base2* x) { return static_cast<Derived&>(*x).data; } int foo2(Base2* x) { return static_cast<Derived*>(x)->data; } int foo3(Base2* x) { Base2& y = *x; return static_cast<Derived&>(y).data; } -- Summary: missed optimization on casting pointers even under -O3 Product: gcc Version: 4.5.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: navin dot kumar at gmail dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45221