How to initialize a template functions with multiple parameter concisely?

Viewed 228

I have a template function with many template parameters, it looks like this:

template <int a, int b, int c>
void foo(){
    for(int i = 0 ; i < a ; i ++)
       for(int j = 0 ; j < b ; j ++)
           for(int k = 0 ; k < c ; k ++)
               //do something
}

In this case, template parameters (a b c) are used so that compiler could unroll these loops. But in practice, these parameters could take a lot of values. Say, if each of a/b/c could take a value from [1,2,3,4], then you get a total of 64 template functions to initialize. So, your code would be like:

if(a == 1 && b == 1 && c == 1) foo<1,1,1>();
else if(a == 1 && b == 1 &&c == 2) foo<1,1,2>();
//......
else foo<4,4,4>();

That's a lot of hideous coding. So, do you have any concise way to do that?

4 Answers
Related