Templated Visitor Base Class

Introduction

There are probably many of you out there who, like me, have investigated the possibilities that templates and template meta-programming can bring to your code.

After having some success with templated image and image algorithm classes, I turned my attention to seeing whether some of the design patterns I use could be encapsulated in the same way. Investigation on the web revealed that template meta-programming could play a part in achieving this.

An article on Dr Dobbs introduced me to typelists and some of the meta-programming techniques, with the Visitor pattern used as an example.

Unfortunately, I soon found that Visual C++ was not entirely happy with the syntax used and it took some time before I managed to get a version that didn’t give the compiler indigestion! That didn’t last long, though. Instantiating my first Visitor object caused the compiler to generate the dreaded “Internal compiler error. Try simplifying your code”. Well, I gave up at that point, deciding I had better things to do with my life than battling my compiler. So, I decided to try and achieve the similar objective to the Visitor example without meta-programming.

Anyone unfamiliar with the Visitor pattern should read this first.

The objective of the Dr Dobbs example was merely to remove a small amount of boilerplate code and ensure that all handling omissions in all code would be flagged at compile time.

The use of this technique in the Visitor pattern may seem somewhat trivial, but I hope that you may find the methods used useful in more complex situations, despite it not being as ‘cool’ as using typelists and meta-programming.

Ok, it’s time to start.

The Classes

This is the null type. It is merely there to act as a place-holder for default template parameters. It’s a template structure that is parameterised with an integer id.

//*****************************************************************
// The null type class.
// For default template parameters.
//*****************************************************************
template <const int id>
struct Null_Type
{
};

This is the class used for actual visitor template parameter types. It’s a pure virtual class and requires a derived class to override the Visit function.

//*****************************************************************
// The virtual visit class.
// Declares a pure virtual visit function for the type.
//*****************************************************************
template<typename T>
class Visit_Type
{
public:
    virtual Visit(T &) = 0;
};

This is a specialised class for the default null visit types. It defines a concrete non-virtual protected function. Derived classes do not need to override it.

//*****************************************************************
// Specialised visit class.
// Declares a non-virtual concrete visit function for null
// visitors.
//*****************************************************************
template <const int id>
class Visit_Type<Null_Type<id> >
{
protected:
    void Visit(Null_Type<id> &);
}

More by Author

Must Read