The purpose of my program is to create a list of data which i can visit with a set of static visitors while using static polymorphism in my class hierarchy.
I have created a hierarchy of classes utilizing static polymorphism through CRTP:
class VirtualBaseData {
public:
//someVirtualFunction
}
template<typename Derived>
class BaseData<Derived> {
public:
template<typename Visitor>
void accept(Visitor &v){
static_cast<Derived*>(this)->accept(v);
}
}
class DerivedBaseData1: BaseData<DerivedBaseData> {
public:
template<typename Visitor>
void accept(Visitor &v){
//Specific implementation
}
}
class DerivedBaseData2: BaseData<DerivedBaseData> {
public:
template<typename Visitor>
void accept(Visitor &v){
//Specific implementation
}
}
I want to store the DerivedBaseData in a contain for later being iterated through and visited.
int main(){
std::vector<VirtualBaseData*> dataSet;
dataSet.push_back(new DerivedBaseData1);
dataSet.push_back(new DerivedBaseData2);
for(auto it = fifth.begin(); it != fifth.end(); ++it){
it->accept(); //Error: VirtualBaseData does not have a member function accept
}
}
I am looking for at a way to couple my static visitors with my static polymorphism hierarchy. I am in need of a VirtualBaseData class in my static polymorphism which is not a template class in order to use the classes in containers. However, since i can not have the VirtualBaseData class be a template class, i am unable to create the appropriate static_cast to a derived class as done in the CRTPattern.
My question is: does anyone have a good strategy which would preserves my static polymorphism setup as well as a static visitor pattern?
For reference: I have implemented my static visitors as described on page 21-23 in http://hillside.net/plop/2006/Papers/Library/portableProgrammingPL.pdf