Re: Virtual method inlining
Am 07.11.2012 11:33, schrieb 1 2:
The compiler may inline a virtual method if it knows the type of an
object. In the following trivial example, f.f() will probably be
inlined because the compiler knows that b is a B.
struct A
{
int n;
virtual void f() { n = 0; }
};
struct B : A
{
void f() { n = 10; }
};
int main()
{
B b;
b.f();
}
Well that's pretty much non-virtual method inlining, because B::f is
not virtual (even though A::f is), not to mention that you're calling
it through a statically-known B instance.
Ehem. B::f *is* virtual. It doesn't require a "virtual" keyword to make
it virtual - it is sufficient to have it virtual in the base class.
Yes, of course here the compiler can see the full type, and that is also
the reason why a compiler could inline it.
In the code I'm writing, there are a lot of cases where the runtime
type is not known statically (after all, that's the whole point of
polymorphism).
In that case, the compiler likely cannot inline it. (Of course, nothing
stops anyone from writing a JIT compiler for C++, but this is rather
untypical, and in classical compiler-linker setups, the compiler does
not know the dynamic type of a polymorphic class.)
Greetings,
Thomas