Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

c++ - 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 virtual functions do not make too much sense.

Is that right?

Can anybody give a case in which an inline virtual function is good?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...