Implementing the visitor pattern using C++ Templates

This can be done in C++11 using variadic templates. Continuing from Pete’s answer: // Visitor template declaration template<typename… Types> class Visitor; // specialization for single type template<typename T> class Visitor<T> { public: virtual void visit(T & visitable) = 0; }; // specialization for multiple types template<typename T, typename… Types> class Visitor<T, Types…> : public Visitor<Types…> … Read more

Visitor Pattern Explanation

Visitor pattern is used to implement double dispatch. In plain words it means that the code that gets executed depends on runtime types of two objects. When you call a regular virtual function, it is a single dispatch: the piece of code that gets executed depends on the runtime type of a single object, namely, … Read more

Visitor pattern’s purpose with examples [duplicate]

So you’ve probably read a bajillion different explanations of the visitor pattern, and you’re probably still saying “but when would you use it!” Traditionally, visitors are used to implement type-testing without sacrificing type-safety, so long as your types are well-defined up front and known in advance. Let’s say we have a few classes as follows: … Read more