Re: Is finalizing a virtual method possible in C++?
Alf P. Steinbach wrote:
* Marcel M?ller:
The line marked with "<---" could never expand the function B::foo()
inline unless the compiler knows that it is final.
On the other side a compiler may at least call any virtual member
function of a class C with a pointer of type C* as 'this' without a
vtable lookup as long as it knows that the function cannot be
overwritten.
That's no reason.
First it's premature optimization until you've measured and found that
the virtual call is a bottleneck. Which is highly unlikely on modern
computers and with modern compilers.
Well, with arguments like this applied to all levels of software
development from the compiler to the application design, you can ensure
that we need more and more computing power even for trivial tasks. (That
is in fact what's happening. But the compilers only have a very small
part in that.)
Check out the committee's
efficiency paper.
The virtual call is no problem. But the missing inlining may be a
significant overhead in some cases. In conjunction with generic
programming trivial functions are more often used.
Second, if against all expectation that call should turn out to be a
bottleneck, one simple cure is to use a non-virtual member function:
Of course, that's a work-around without runtime overhead.
However, it's unclear to provide two interfaces. One for base class
users and another for the others.
But you are right, we should not overstate that topic.
Marcel