Say I have a template function that accepts variable arguments and calls other functions with those arguments...
template<typename... A> func(int i, A... args)
{
// do something common
switch (i)
{
case 0: x(args...); break;
case 1: y(args...); break;
case 2: z(args...); break;
default: break;
}
// do something common
}
...and the other functions are defined as...
void x(int a, int b);
void y(int a, int b); // note: takes same args as x()
void z(std::string a);
This doesn't compile because calling func() with a std::string argument will not find a match for x() and y() that takes a std::string argument.
Note that func() uses some external criteria (the value of parameter i in this example) to decide which other function to pass parameter args to.
Is there a way to pull this off without resorting to something like stuffing parameter args into a std:tuple and passing that?
[EDIT]
Ok, I figured out an approach that works well enough for my purposes. Here's an example class to illustrate...
class X
{
private:
bool a(int) { return true; }
bool b(int) { return true; }
template<typename... A> bool a(const A&...) { return false; }
template<typename... A> bool b(const A&...) { return false; }
public:
template<typename... A> void x(int i, const A&... args)
{
if (i == 0)
{
a(args...);
}
else
{
b(args...);
}
}
};
The variadic template methods for a() and b() will catch any calls made to X.x() where the arguments are not a single int. This resolves the compiler error I was receiving and enables the simpler class API that I was looking for.