templated-implementation-of-the-visitor-pattern-for-c11
templated-implementation-of-the-visitor-pattern-for-c11
templated-implementation-of-the-visitor-pattern-for-c11
The new code uses ‘variadic temples’ that were introduced with C++11, and as such, greatly simplifies the code required.
____________________________________________________________________________________________________
Visitable

//*****************************************************************
/// The visitable class for N types.
//*****************************************************************
template <typename T1, typename... Types>
class Visitable : public Visitable<T1>, public Visitable<Types...>
{
public:

  using Visitable<T1>::Accept;
  using Visitable<Types...>::Accept;
};

//*****************************************************************
/// The specialised visitable class for 1 type.
//*****************************************************************
template <typename T1>
class Visitable<T1>
{
public:

  virtual void Accept(T1&) = 0;
};
____________________________________________________________________________________________________
Visitor

//*****************************************************************
/// The visitor class for N types.
//*****************************************************************
template <typename T1, typename... Types>
class Visitor : public Visitor<T1>, public Visitor<Types...>
{
public:

  using Visitor<T1>::Visit;
  using Visitor<Types...>::Visit;
};

//*****************************************************************
/// The specialised visitor class for 1 type.
//*****************************************************************
template <typename T1>
class Visitor<T1>
{
public:

  virtual ~Visitor() = default;

  virtual void Visit(T1&) = 0;
};