I'm making a library which requires that classes must inherit other classes to do something specific. However, this is not simple polymorphism. These classes are code generators of virtual functions, with no data and which rely on CRTP, so they themselves don't need a vtable.
Is there a way to stop the vtable from being emitted for these classes? I'd assume that the virtual function pointers would be passed the the derived class, and the virtual destructor would just skip over these classes. Sort of like melding the classes together into one.
If nothing general is available across the C++ domain, then maybe specific to clang, gcc and vc?
Example:
#include<iostream>
template <typename D, typename B>
struct jelly : B
{
virtual void do_stuff() { static_cast<D*>(this)->D::do_some_other_stuff(); }
};
template <typename D>
struct jelly<D, void>
{
virtual void do_stuff() { static_cast<D*>(this)->D::do_some_other_stuff(); }
};
struct A : jelly<A, void>
{
void do_some_other_stuff() { std::cout << "A::do_some_other_stuff()\n"; }
};
struct B : jelly<B, A>
{
void do_some_other_stuff() { std::cout << "B::do_some_other_stuff()\n"; }
};
int main()
{
A a;
a.do_stuff(); // output: A::do_some_other_stuff()
B b;
b.do_stuff(); // output: B::do_some_other_stuff()
A& aa = b;
aa.do_stuff(); // output: B::do_some_other_stuff()
}
Just to clarify, this is just an example. It does run, but the number of classes that jelly
represents is actually 3 different ones. One that is inherited explicitly by the dev using the jelly
library, and 2 others that are done implicitly, before inheriting back into the dev's own classes. It is because of the number of classes would increase 3x that it has become to worry me, and is why I am asking this question.