I have the following function which can take N arguments of different types, and forwards them to N functions templated on each individual type, in this manner (example with two arguments):
template <typename T1, typename T2>
bool func(int& counter, T1 x1, T2 x2) {
switch (counter) {
case 0:
if (func2<T1>(x1)) {
counter++;
return true;
} else {
return false;
}
case 1:
if (func2<T2>(x2)) {
counter++;
return true;
} else {
return false;
}
default:
return true;
}
}
I want to write this function with variadic templates so that it can handle any number of arguments in a type-safe way. I can see a solution using recursive functions, passing along the counter and the variadic index and comparing them for equality, but this would seem to yield far less efficient code than the switch statement above (a sequence of if-checks compared to a jump table).
Can this be done efficiently using template metaprogramming or do I need to provide overloads for each arity?