If you want specific functions from runtime variable then use a map instead. Template is the wrong tool for the job as you have to write lot just to transform variable into constants.
Assuming that you enum have a default value None and that you have up to say 5 arguments, you can define a map like this:
enum MyEnum { None = 0, A, B, C, D... };
using MyKey = std::tuple<MyEnum, MyEnum, MyEnum, MyEnum, MyEnum>;
using MyFunction = std::function<void()>;
Then you have somewhere a map of function (a singleton)
std::map<MyKey, MyFunction> myMap;
A utility function could be helpful to create a key from a variable number of arguments:
MyKey MakeKey(MyEnum e1, MyEnum e2 = None, MyEnum e3 = None, MyEnum e4 = None, MyEnum e5 = None)
{
return std::make_tuple(e1, e2, e3, e4, e5);
}
myMap.emplace(MakeKey(A, B), [](){ /* some code */ });
MyEnum AtOrDefault(const vector<enum> &eVec, int index)
{
return index < eVec.size() ? eVec[index] : None;
}
Then assuming you want to call the appropriate function from a vector, you could do:
void f(const vector<enum> &eVec)
{
if (eVec.size() > 5) throw some_exception;
MyKey key = std::make_typle(
AtOrDefault(eVec, 0),
AtOrDefault(eVec, 1),
AtOrDefault(eVec, 2),
AtOrDefault(eVec, 3),
AtOrDefault(eVec, 4));
auto &fn = myMap[key];
fn();
}
You could also use the idea of calculating a value assuming that you know the maximum number of elements in the enum. You could then create a CombinedEnumType:
enum CombinedEnumType : uint32_t { };
And defined a function
CombinedEnumType MakeCombinedEnumType(MyEnum e1, … MyEnum e5 = None)
{
const int MyEnumEcount = 5;
MyEnum a[] = { e1, e2, e3, e4, e5 };
uint32_t result = 0;
for (auto & item : a) { result *= MyEnumEcount; result += item; }
return static_cast<CombinedEnumType>(result);
}
This code is just for ideas. In real production code, you have to use constants, proper variable names, validate that a function exist for a given combination…