This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: efficiency of calling one virtual member of class from another


I often wish C++ had a feature similar to "final" (if I remember correctly) in Java. You ought to be able to declare a virtual override as the final one (cannot be overridden again in any subclasses of Sub1). But C++ has no such feature.

It is almost impossible for the optimizer to have enough information for it to avoid using the vftable in the example you gave.

But I think you can directly force it to skip the vftable by using

void *r = Sub1::f(a+1);

instead of

void *r = f(a+1);

Then the optimizer is free to inline f in g, as you seem to want.

The meaning of that construct is that even if you derived a class from Sub1 and even if that class overrode f and didn't override g, then when you call g for an object of that derived class it will use the f from Sub1 instead of the f from that derived class.

I know you don't intend to derive such a class. But telling the compiler in this way how such a class would be handled in that case lets it generate more optimal code for the case you care about.

Bill Mann wrote:


I have not and do not intend to define any subclasses of Sub1 or the other subclasses at this level. Base and all the subclasses are in the same .h file.

The question is, what happens when I call f() from g()?  At -O3?  Does
it go though the usual vector dispatch table,



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]