Hopefully this isn\'t too specialized of a question for StackOverflow: if it is and could be migrated elsewhere let me know...
Many moons ago, I wrote a und
Is there anything in Standard C++ which would prevent a compiler from performing such a transformation?
Not if you're sure the observable behavior is unchanged - that's the "as-if rule" which is Standard section 1.9.
But this might make proving that your transformation is correct pretty difficult: 12.7/4:
When a virtual function is called directly or indirectly from a constructor (including the mem-initializer or brace-or-equal-initializer for a non-static data member) or from a destructor, and the object to which the call applies is the object under construction or destruction, the function called is the one defined in the constructor or destructor's own class or in one of its bases, but not a function overriding it in a class derived from the constructor or destructor's own class, or overriding it in one of the other base classes of the most derived object.
So if the destructor Foo::~Foo()
happens to directly or indirectly call C::quack()
on an object c
, where c._f
points at the object being destroyed, you need to call Foo::bark()
, even if _f
was a FooA
when you constructed object c
.
On first reading, this sounds like a c++-focused variation of polymorphic inline caching. I think that V8 and Oracle's JVM both use it, and I know that .NET does.
To answer your original question: I don't think there's anything in the standard that forbids these kinds of implementations. C++ takes the "as-is" rule quite seriously; so long as you faithfully implement the right semantics, you can do the implementation in any crazy way you like. c++ virtual calls aren't very complicated, so I doubt you'd trip over any edge cases there either (unlike if, say, you were trying to do something clever with static binding).