I thought it would be a simple exercise to write a generic visitor base class template. The goal is to be able to write
typedef visitor<some_base, some_derived1, some_derived2> my_visitor;
...and then have my_visitor be a type that is functionally equivalent to
struct my_visitor {
virtual void visit(some_base&) {}
virtual void visit(some_derived1&) {}
virtual void visit(some_derived2&) {}
};
which i can inherit with actually useful derived visitor classes for that type hierarchy, which override the different visit() versions as needed. I want it to work for any number of types having any inheritance relations, and i don't want to use any hacks that reimplement virtual functions using type_info comparisons. This is what I came up with:
#include <cstdlib>
#include <iostream>
#include <vector>
/** This is the generic part that would go in a visitor.hpp header. */
template <typename T> struct visitor_base {
virtual void visit(T&) {};
};
template <typename... T> struct visitor : visitor_base<T>... {};
/** This is the part that is specific to a certain type hierarchy. */
struct base;
struct derived1;
struct derived2;
typedef visitor<base, derived1, derived2> my_visitor;
/** This is the type hierarchy. */
struct base {
virtual void accept(my_visitor& v) { v.visit(*this); }
};
struct derived1 : base {
derived1() : i(42) {}
virtual void accept(my_visitor& v) { v.visit(*this); }
int i;
};
struct derived2 : base {
derived2() : f(2.79) {}
virtual void accept(my_visitor& v) { v.visit(*this); }
float f;
};
/** These are the algorithms. */
struct print_visitor : my_visitor {
void visit(base&) { std::cout<<"that was a base."<<std::endl; }
void visit(derived1& d) { std::cout<<"that was "<<d.i<<std::endl; }
void visit(derived2& d) { std::cout<<"that was "<<d.f<<std::endl; }
};
struct randomise_visitor : my_visitor {
void visit(derived1& d) { d.i = std::rand(); }
void visit(derived2& d) { d.f = std::rand() / float(RAND_MAX); }
};
int main() {
std::vector<base*> objects { new base, new derived1, new derived2,
new derived2, new base };
print_visitor p;
randomise_visitor r;
for (auto& o : objects) o->accept(p);
for (auto& o : objects) o->accept(r);
for (auto& o : objects) o->accept(p);
}
The problem is that this does not compile. GCC says
silly_visitor.cpp: In member function ‘virtual void base::accept(my_visitor&)’:
silly_visitor.cpp:24:42: error: request for member ‘visit’ is ambiguous
silly_visitor.cpp:8:16: error: candidates are: void visitor_base<T>::visit(T&) [with T = derived2]
silly_visitor.cpp:8:16: error: void visitor_base<T>::visit(T&) [with T = derived1]
silly_visitor.cpp:8:16: error: void visitor_base<T>::visit(T&) [with T = base]
Basically, the problem is that since the different visit() member functions are declared in different classes, they are not seen as candidates for overload resolution but just as ambiguous member access. The normal trick for forcing the compiler to consider inherited functions for overload resolution is to redeclare them in the derived class with 'using' statements, but that is not feasible in this case since it would ruin the whole generic aspect of it.
So, apparently this was not as easy as I thought. Any ideas?