I have a template function where the template parameter is an integer. In my program, I need to call a function with a small integer that is defined at runtime. By hand I can make a table, for example:
void (*f_table[3])(void) = {f<0>,f<1>,f<2>};
and call my function with
f_table[i]();
Now the question is whether there is some automatic way to build this table in random order. The best I can come up with is to use a macro
#define TEMPLATE_TAB(n) {n<0>,n<1>,n<2>}
which at least avoids repeating the function name over and over (my real functions have longer names than "f"). However, the maximum allowable order is still hardcoded. Ideally, the size of the table should be determined by only one parameter in the code. Is it possible to solve this problem using templates?
source
share