Dispatching on a variant

Lutger lutger.blijdestijn at gmail.com
Sat Sep 26 09:28:52 PDT 2009


Justin Johansson wrote:

...
> 
> I've got about 2 dozen types in the variant so the O(n) really hurts.
> The variant thing seemed like a really cool idea at the time but now ...
> Without something like suggested above or a computed goto on typeid or
> Andrei's visitator, it almost pushes me to backout from using variants and
> having to redesign around some common base class or interface and using
> virtual function dispatch. :-(

You can use a hash literal as a jump table but it's an unsafe hack and 
probably not performant depending on what you need to do. Maybe it's easier 
still to write the visitation code yourself or generate code with ctfe than 
to redesign to common base classes.

a hash literal works like this, index with the typeid to get a function ptr 
you can call:

Algebraic!(int, Foo) a;
a = 3;
[ typeid(int) : function { writeln("a is int"); },
  typeid(Foo) : function { writeln("a is Foo"); }
] [a.type] ();



More information about the Digitalmars-d mailing list