I am using a class from a 3rd party library which look like,
template <typename A = DefaultT, typename B = DefaultT, typename C = DefaultT, typename D = DefaultT, typename E = DefaultT, typename F = DefaultT>
class Vertex {};
I want to use a partial specialization of this class at runtime depending on conditions, for example,
class MyA {};
class MyB {};
class MyC {};
class MyD {};
bool useA, useB, useC, useD; //these booleans change at runtime
// I want to specialize Vertex depending on the above booleans
// The below line shouldn't compile, this is just to give an idea
typedef typename Vertex <useA ? MyA : DefaultT, useB ? MyB : DefaultT,
useC ? MyC : DefaultT, useD ? MyD : DefaultT> MyVertex;
I want to conditionally choose which template arguments I want to specialize. I am not sure if there's a term for this problem, I doubt it's a flavor of multiple dispatch.
A simple way would be to write 15 (2^4 -1) classes like,
typedef typename Vertex <MyA> MyVertexWithA;
typedef typename Vertex <DefaultT, MyB> MyVertexWithB;
typedef typename Vertex <MyA, MyB> MyVertexWithAB; //and so on...until
typedef typename Vertex <MyA, MyB, MyC, MyD> MyVertexWithABCD;
The problem becomes more complicated because I have to use a 'Mesh' class which uses the specialized vertex class
template <typename VertexClass, typename Others>
class Mesh {};
Now if I went down the path of writing 15 classes then I would have to write 15 more lines for each different mesh type. And it keeps getting more complicated where Mesh class is used.
I strongly believe this has to be done by either me or the compiler. My questions:
- I want to know if there's a way to make the compiler do this work for me?
- Does C++11 have a better mechanism to handle this scenario? Thanks.