6

Does C++ have a proper implementation of interface that does not use vtable?

for example

class BaseInterface{
public:
virtual void func() const = 0;
}

class BaseInterfaceImpl:public BaseInterface{
public:
void func(){ std::cout<<"called."<<endl; }
}

BaseInterface* obj = new BaseInterfaceImpl();
obj->func();

the call to func at the last line goes to vtable to find the func ptr of BaseInterfaceImpl::func, but is there any C++ way to do that directly as the BaseInterfaceImpl is not subclassed from any other class besides the pure interface class BaseInterface?

Thanks. Gil.

gilbertc
  • 1,049
  • 1
  • 10
  • 19
  • Why would you care if it goes through the vtable? – Thomas Apr 06 '10 at 19:14
  • coz i am working on a project where speed does matter. also, i think vtable is a great solution for full oo polymorphism but an overkill for implementing 'interface' and i hope there would be some lighter solution exists.. – gilbertc Apr 06 '10 at 19:24
  • @gilbertc: I think there is a misunderstanding here. *Any* "proper" implementation of the concept of interfaces will go through something like a vtable. Otherwise, dynamic polymorphism would be impossible. – Thomas Apr 06 '10 at 19:24
  • 1
    I would have to say, if you are concerned about polymorphism overhead than you are either using the wrong language or using the language in the wrong way. Profile and see where you are eating cycles. It would be very surprising if it was in the vtable. – Joel Apr 06 '10 at 20:10
  • @Joel: It is perfectly possible for that overhead to be problematic. That doesn't mean the OP is doing anything wrong. (But of course, without profiling and actually confirming that it is a problem, this falls under the heading of premature optimization) – jalf Apr 06 '10 at 22:07
  • Have you actually meassured? The virtual dispatch mechanism (in simple cases as this one) is *really* fast, usually a single dereference. Then again, if there is only one possible implementation, what is the point of having an interface? – David Rodríguez - dribeas Apr 06 '10 at 22:41
  • @David: for future changes ? It's easier to create an interface and an implementation.. even if no other implementation is ever needed.. than to create a simple class and realize you would need another implementation (for unit testing for example). – Matthieu M. Apr 07 '10 at 07:36
  • @Matthieu: I agree, but if (which I find hard to believe) the virtual dispatch cost is noticeable and there is a single implementation the simple thing to do is removing the interface. (Not that I believe that the virtual dispatch will have any effect in performance in most cases) – David Rodríguez - dribeas Apr 07 '10 at 08:34

2 Answers2

7

Yes. It goes by the moniker CRTP. Have a gander.

wheaties
  • 35,646
  • 15
  • 94
  • 131
  • In particular, the heading "Static polymorphism": http://en.wikipedia.org/wiki/Template_metaprogramming#Static_polymorphism Maybe you could edit that example into your answer? – Thomas Apr 06 '10 at 19:15
  • No problem. I'm quite curious, your code is that optimized where even the small overhead of a vtable look-up is hurting performance? What application? Normally it's used as a space saver or in an implementation of RAII concern. – wheaties Apr 06 '10 at 20:59
0

I think in any language, it's going to have to go to some equivalent of a vtable in order to do dynamic dispatch unless it knows at compile time what function needs to be called. This could be the result of a clever compiler optimization, or a technique such as CRTP (which wheaties already mentioned).

rmeador
  • 25,504
  • 18
  • 62
  • 103
  • and by the way, I am unable to understand what about going through the vtable makes such a call "improper". – rmeador Apr 06 '10 at 19:18
  • @thomas, rmeador: agreed that i shouldn't use the word proper. should be along the direction of 'more efficient' and 'light' implementation of interface. – gilbertc Apr 06 '10 at 19:26