As you can read in the papers about the visitor pattern, it is a very controvers one. On one side you use it to reduce compile time for a hierarchy of classes, on the other it becomes a bottleneck itself if you change something of the hierarchy, e.g. if you add a new class. (see gof, alexandrescu et al.).
This implementation uses a template trick to push the instantiation forward to handle this problem. It is a synthesis of Robert Martins dynamic cast solution and Barton and Nackmans Mixin. I call the trick "deferred downcast" for itself...
A graph:
Well, beside the trick its a conventionell implementation. The base class (here: DocElement) has a derived class which is shielded with the Visitoracceptor class against the visitors access. This one makes the downcast - typesafe, fast. If todays compiler would be up to date, we would not need more than the template itself. No second hierarchy of classes. But they aren't, so we need from time to time help of a macro. This specializes the accept method and is normally used only where the full knowledge is needed - inside the factorys impls.
The visitor class ist boiled down to a minimum. Only the types for the return value and the class from which the real visitors are derived. A virtual dtor, too. There we have the second downcast.
I used a adaptor pattern for decoupling the visitor class from the implementations. So they are immune against new classes in the hierarchy. Mostly they dont need to be recompiled. You can use them like a LEGO system...
The test drivers have been run under GNU gcc 3.0 and Visual C++ 6.0.