C++ generic double-dispatcher/visitor I'm trying to reduce the boilerplate code for the visitor/double dispatcher pattern. First I've created a way to enforce the dispatcher to declare the dispatch(T) function for every type supported and also a dispatch_gen(T) for the generic case which I don't need to implement. This is not too hard:

/*
A generic way to get the type of a base class using a tag function
*/
template &lt;typename T, typename U&gt;
T base_of(U T::*);

/*
Generic dispatcher
*/
template &lt;typename... T&gt;
struct double_dispatcher_impl;

template &lt;typename Head&gt;
struct double_dispatcher_impl&lt;Head&gt; {
    virtual void dispatch(Head*) = 0;
};

template &lt;typename Head, typename... Tail&gt;
struct double_dispatcher_impl&lt;Head, Tail...&gt; : public double_dispatcher_impl&lt;Tail...&gt; {
    using double_dispatcher_impl&lt;Tail...&gt;::dispatch;

    virtual void dispatch(Head*) = 0;
};

template &lt;typename... T&gt;
struct double_dispatcher;

template &lt;typename Head, typename... Tail&gt;
struct double_dispatcher&lt;Head, Tail...&gt; : public double_dispatcher_impl&lt;Head, Tail...&gt; {
    using Base = decltype(base_of(&amp;Head::dispatch_id));
    using double_dispatcher_impl&lt;Head, Tail...&gt;::dispatch;

    virtual void dispatch_gen(Base* b) { b-&gt;dispatch(this); }
};


The dispatch_id() is just a hacky way to get the base class, it server as a tag. Now the base class for a visitable/dispatchable:

template &lt;typename D&gt;
struct double_dispatchable {
    virtual void dispatch(D* d) { }
    void dispatch_id() { }
};


This gives me a relatively "nice" usage. Here's a simple example:

struct Circle;
struct Rect;
struct Triangle;
typedef double_dispatcher&lt;Circle, Rect, Triangle&gt; shape_disp;

struct Shape : public double_dispatchable&lt;shape_disp&gt; { };
struct Circle : public Shape    { void dispatch(shape_disp* disp) { disp-&gt;dispatch(this); } };
struct Rect : public Shape      { void dispatch(shape_disp* disp) { disp-&gt;dispatch(this); } };
struct Triangle : public Shape  { void dispatch(shape_disp* disp) { disp-&gt;dispatch(this); } };

struct my_dispatcher : public shape_disp {
    void dispatch(Circle* c)    { std::cout &lt;&lt; "Circle!" &lt;&lt; std::endl; }
    void dispatch(Rect* c)      { std::cout &lt;&lt; "Rect!" &lt;&lt; std::endl; }
    void dispatch(Triangle* c)  { std::cout &lt;&lt; "Triangle!" &lt;&lt; std::endl; }
};


Now there's still a lot of repetitive code in there (the callbacks for the subclasses). How could I improve the pattern? Can I remove the repetitive code with metaprogramming and generics, without using macros?
I have implemented something like that in one of my projects, the trick is to use CRTP to pass the Child type to the parent.

This is a simplified code :

#include "collisions.h"
#define SHAPES Box, Sphere, Mesh
class Box;
class Sphere;
class Mesh;

class Geometry;

// Collidable defines all the pure virtual functions.
template &lt;typename... Shapes&gt;
struct Collidable;

template &lt;&gt;
struct Collidable&lt;&gt; {
    virtual bool collide(const Geometry* obj) const = 0 ;
};

template &lt;typename Shape, typename... Others&gt;
struct Collidable&lt;Shape, Others...&gt; : Collidable&lt;Others...&gt; {
    using Collidable&lt;Others...&gt;::collide;
    virtual bool collide(const Shape* obj) const = 0;
};

// Geometry inherits from Collidable and is our common base class.
class Geometry : public Collidable&lt;SHAPES&gt; {
    public:
        Geometry();
        virtual ~Geometry();
        glm::vec3 getPosition() const;

    private:
        glm::vec3 m_pos;
};

// Dispatcher inherits from Geometry and implements one dispatch function
// for all the given type and the Geometry one.
template &lt;class T, typename... Shapes&gt;
struct Dispatcher;

template &lt;class T&gt;
struct Dispatcher&lt;T&gt; : public Geometry {
    bool collide(const Geometry* obj) const final override {
        return obj-&gt;collide(static_cast&lt;const T*&gt;(this));
    }
};

template &lt;class T, typename Shape, typename... Others&gt;
struct Dispatcher&lt;T, Shape, Others...&gt; : public Dispatcher&lt;T, Others...&gt; {
    using Dispatcher&lt;T, Others...&gt;::collide;
    bool collide(const Shape* obj) const final override {
        return Collisions::collide(obj, static_cast&lt;const T*&gt;(this));
    }
};

// Shape&lt;T&gt; is our generic dispatch class.
template &lt;class T&gt;
class Shape : public Dispatcher&lt;T, SHAPES&gt; {};


With all shapes defined like:

class Sphere : public Shape&lt;Sphere&gt; {...};

Of course, all the corresponding Collisions::collide(*,*) must be declared.
I am not very familiar with template meta-programming, so I couldn't find a clean solution to replace the #define SHAPES Box, Sphere, Mesh and the classes declarations at the beginning of the file.

Furthermore, I don't know if a good compiler can optimize virtual calls using de-virtualization (that's why I tried to add final keyword).