I want to construct boost::variants containing default-constructed values, specified with a type index – without writing my own switch statement over the type index.
I figure this must be possible, somehow, with MPL?
To clarify though, the index is not a compile-time constant expression.
The use case is that I need to construct a variant which will later be replaced with one containing the correct value, but at this point I only know the type index. Think of it as a lazy deserialisation problem.
You need to use the
variant::typestypedef. This gives you an MPL compatible sequence which we can then use withmpl::atand a template to do our bidding. This does the trick:Here goes the runtime-variant:
It’s a little arcane and will need some tweaking with variadic
arguments to be truly generic, but it does what you want. Someone else needs to figure out if this results in significant
code blow-up.