I have multiple factories, each is responsible for creating instances of an inheritance tree. For example (syntax or semantics maybe incorrect),
struct InterfaceA {}; struct ImplA1 : InterfaceA {}; ...
struct FactoryA { vector<InterfaceA*> create(); }
... // (repeat for B, C, etc)
foo is template class
template<ATy, BTy, ..>
struct foo {
// default behavior - no functionality
};
foo is specialized for some combination of subclasses of InterfaceA, InterfaceB, and ...
template<>
struct foo<ImplA1, ImplB2, ..> {
void feature1();
void feature2();
};
Is it possible to combine the inheritance hierarchies with the template class? i.e.
void bar(vector<InterfaceA*> vecA, vector<InterfaceB*> vecB, ..) {
foo< /* what to put in here */ >(vecA.front(), vecB.front(), ...) f;
f.feature1(); // if specialization not there, complain
}
I can use Curiously Recurring Template Pattern (CRTP) for one tree. Not sure how to get it work with multi trees.
vecA.front()andvecB.front()are clearly known. And if you need to instantiate atemplatebased on their dynamic type – well, you can't do that…vecA.front()returns an item of typeInterfaceA*. It can point to anImplA1,ImplA2or any other derived class ofInterfaceA.