- How expensive is a virtual function call and why?
While not very expensive in absolute numbers (a few nanoseconds at most), a virtual function call is several times more expensive than a non-virtual one and could easily be an order of magnitude or more slower than an inline function call. The overhead comes from the indirection: a virtual function is always invoked by a function pointer, and the actual function is unknown at compile time and cannot be inlined.
- Why does a similar function call, resolved at compile time, have no such performance overhead?
If the compiler knows the exact function that is going to be called, it can optimize away the indirection and may be able to inline the function.
- How can we make compile-time polymorphic function calls?
Just like the runtime polymorphic calls are made through the pointer to the base class, the static polymorphic...