inline virtual function
In C++, my understanding is that virtual function can be inlined, but generally, the hint to inline is ignored. It seems that inline virtu开发者_如何学Pythonal functions do not make too much sense.
Is that right?
Can anybody give a case in which an inline virtual function is good?
In order to answer this question fully, one needs to understand that the property of being virtual
applies independently to the function itself and to the calls made to that function. There are virtual and non-virtual functions. There are virtual and non-virtual calls to these functions.
The same is true about the property of being inline
. There are iniline and non-inline functions. And there are inlined and non-inlined calls to these functions.
These properties - virtual
and inline
- when applied to the function itself, do not conflict. They simply have no reason and no chance to conflict. The only thing that inline
specifier changes for the function itself is that it modifies the One Definition Rule for that function: the function can be defined in multiple translation units (and it has to be defined in every translation unit where it is used). The only thing virtual
specifier changes is that the class containing that function becomes polymorphic. It has no real effect on the function itself.
So, there's absolutely no problem in declaring a function virtual
and inline
at the same time. There's no basis for the conflict whatsoever. It is perfectly legal in C++ language.
struct S {
virtual void foo();
};
inline void S::foo() // virtual inline function - OK, whatever
{
}
However, when people are asking this question, they are usually not interested in the properties of the function itself, but rather in characteristics of the calls made to the function.
The defining feature of a virtual call is that it is resolved at run time, meaning that it is generally impossible to inline true virtual calls:
S *s = new SomeType;
s->foo(); // virtual call, in general case cannot be inlined
However, if a call is by itself non-virtual (even though it goes to a virtual function), inlining is not a problem at all:
S *s = new SomeType;
s->S::foo(); // non-virtual call to a virtual function, can easily be inlined
Of course, in some cases an optimizing compiler might be able to figure out the target of a virtual call at compile time and inline even such virtual call. In some cases it is easy:
S ss;
ss.foo(); // formally a virtual call, but in practice it can easily be inlined
In some cases it is more complicated, but still doable:
S *s = new S;
s->foo(); // virtual call, but a clever compiler might be able
// to figure out that it can be inlined
Under normal circumstances, a virtual function will be invoked via a pointer to a function (that's contained in the class' vtable). That being the case, a virtual function call can only be generated inline if the compiler can statically determine the actual type for which the function will be invoked, rather than just that it must be class X or something derived from X.
The primary time an inline virtual function makes sense is if you have a performance critical situation, and know that a class will frequently be used in a way that allows the compiler to determine the actual type statically (and at least one target compiler optimizes out the call via pointer).
You can have virtual functions as inline. The decision to make the function call inline is not just made at the compile time. It could be anytime between compilation to rutime. You can refer to this article from Herb Sutter. Inline Redux
精彩评论