Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it always true that virtual function cannot be inlined in C++?

It is said that virtual functions cannot be inlined. Is it always true that if a function is declared virtual, it cannot be inlined anywhere in the code, or is it applicable only under certain situations? (eg., calling method from a base pointer vs. calling method on a reference, etc.)

like image 700
paseena Avatar asked Apr 05 '11 21:04

paseena


2 Answers

No, virtual functions can indeed be inlined. Virtual dispatch is only used when calling a virtual method polymorphically (i.e., on a pointer or reference to an object). However, when a virtual method is called on an object value, virtual dispatch is not used and the compiler is free to inline as it sees fit.

like image 167
ildjarn Avatar answered Nov 08 '22 20:11

ildjarn


Given:

struct T {
   virtual void foo() { /* something */ }
};

Using polymorphism (If you're calling foo() through a pointer-to-T or a reference-to-T)

T* ptr = get_ptr_somehow();
ptr->foo();

If a compiler knows that T is the only node in its inheritance tree, it could forego the virtual calls and potentially inline the function. However, this is an incredibly unlikely scenario and I doubt whether it could even feasibly be detected. In all other cases, inlining is not practical due to the run-time dispatch.

Static dispatch (If you're calling foo() on a bog-standard object)

T obj;
obj.foo();

In this case, the compiler knows that the dynamic type of obj is T, that it does not require virtual dispatch, and therefore may inline the function code if it wants to.

T* ptr = get_ptr_somehow();
ptr->T::foo();

In this case, the compiler doesn't know the dynamic type of obj, but it knows which function it's going to call, knows that it does not require virtual dispatch, and therefore may inline the function code if it wants to.

like image 42
Lightness Races in Orbit Avatar answered Nov 08 '22 19:11

Lightness Races in Orbit