For a templated function that takes an integer, I wrote the following general-purpose dispatcher:
#define Dispatch_Template(funct_name, max) \
template<int i> decltype(&funct_name<0>) Dispatch_template_##funct_name (int index) { \
return (index == i) ? funct_name <i> : Dispatch_template_##funct_name <i - 1>(index); \
} \
template<> decltype(&funct_name<0>) Dispatch_template_##funct_name <-1>(int) { \
return nullptr; \
} \
decltype(&funct_name<0>) Dispatch_##funct_name (int i) { \
return Dispatch_template_##funct_name <max>(i); \
} \
This works and I can do something like this:
template<int some_int> void PrintInt() {
printf("int is %i\n", some_int);
}
Dispatch_Template(PrintInt, 6);
int main()
{
for (int i = 0; i < 6; ++i) {
Dispatch_PrintInt(i)();
}
return 0;
}
But what if I want to pass a typename parameter to my templated function?
For example, say it looks like this:
template<int some_int, typename some_type> void PrintSomeType(some_type arg) {
// do something
}
I want to be able to do this:
template<typename some_type> void caller(some_type arg) {
Dispatch_Template(PrintSomeType, some_type, 6);
for (int i = 0; i < 6; ++i) {
Dispatch_PrintSomeType(i)(arg);
}
}
I'm not sure how to do this - I'm running into issues with "a template declaration is not allowed here". (Note that Dispatch_Template here has to be inside the function because the function itself is templated.)