jeudi 1 novembre 2018

C++ : How to programmatically define template types in runtime?

I have a requirement where there's a enum and there are template functions defined for all the possible enum combinations upto length l.

Say the enum is

Enum typenum {A, B, C}

And all these template functions are defined and available at runtime (i.e, compiler creates these functions at the compile time)

Alpha<A>::f()
Alpha<B>::f()
Alpha<C>::f()
Alpha<A,A>::f()
Alpha<A,B>::f()
Alpha<A,C>::f()
Alpha<B,A>::f()
Alpha<B,B>::f()
Alpha<B,C>::f()
Alpha<C,A>::f()
Alpha<C,B>::f()
Alpha<C,C>::f()
and combination of 3 enums, 4 enums...

Now I've to choose the right function as per an input vector

void f(vector<enum> eVec){
    Alpha::f<eVec[0], eVec[1],... eVec[eVec.size() - 1]>() // <-------

How do I do this? One way to do this would be to define for every size. Eg:

if(eVec.size() == 1)
   Alpha<eVec[0]>::f()
else if(eVec.size() == 2)
   Alpha<eVec[0], eVec[1]>::f()

This won't scale though. Is there any elegant, scalable way of doing this.

Aucun commentaire:

Enregistrer un commentaire