Multiple dispatch with variant and multi visitation in C++ The following program is a demonstration of variant and double visitation used to obtain double dispatch between the elements of two inhomogeneous containers. Most of the work is done at compile time. What do you think about it compared to the dynamic version in explained in Wikipedia?

boost::variant does not employ variadic templates and has a hand coded limit to 20 different types which can be easily extended to 50 but not beyond. Is it a crazy idea to use this pattern (with an appropriate variant implementation) with more than 50 types, say 100 spaceships and 10 different asteroids or so?

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include "boost/variant.hpp"
using std::cout;
using std::endl;

////////// some basic objects

class SpaceShip {};
class ApolloSpacecraft : public SpaceShip {};

class Asteroid {
public:
  void CollideWith(SpaceShip&amp;) 
       { cout &lt;&lt; "Asteroid hit a SpaceShip" &lt;&lt; endl; }
  void CollideWith(ApolloSpacecraft&amp;) 
       { cout &lt;&lt; "Asteroid hit an ApolloSpacecraft" &lt;&lt; endl; }
};

class ExplodingAsteroid : public Asteroid {
public:
  void CollideWith(SpaceShip&amp;) 
       { cout &lt;&lt; "ExplodingAsteroid hit a SpaceShip" &lt;&lt; endl; }
  void CollideWith(ApolloSpacecraft&amp;)
       { cout &lt;&lt; "ExplodingAsteroid hit an ApolloSpacecraft" &lt;&lt; endl; }
};
////////// visitor

struct CollideVisitor : public boost::static_visitor&lt;void&gt; {
  template &lt;typename A, typename S&gt;
  void operator()( A &amp; a, S &amp; s) const { a.CollideWith(s);}
};

////////// demo

int main() {
  std::vector&lt;boost::variant&lt;Asteroid,ExplodingAsteroid&gt;&gt; asteroids;
  asteroids.emplace_back(Asteroid());
  asteroids.emplace_back(ExplodingAsteroid());

  std::vector&lt;boost::variant&lt;SpaceShip,ApolloSpacecraft&gt;&gt; spaceships;
  spaceships.emplace_back(SpaceShip());
  spaceships.emplace_back(ApolloSpacecraft());

  for (auto &amp; a : asteroids) {
    for (auto &amp; s : spaceships) {
      boost::apply_visitor(CollideVisitor(), a, s);
    }
  }

  return 0;
}
It's a good example of multimethods in C++. But you should be aware that variant takes at least as much space as the biggest of its argument. It's done so to minimize overhead of walking an additional pointer. Probably, you need a variant with another allocation policy, as asteroids and spaceships are quite numerous. 

But, to be serious, there's an even bigger problem: extension. You would like to


define new functions that do something with asteroids, spaceships or tuples of them;
define new types of asteroids and spaceships;
modify code only locally.


Your solution needs to have variant&lt;list, of, all, objects, of, the, kind&gt; in several places of your code in addition to defining a new classes for new types of game objects. That's a non-local change.