I am new to template meta-programming but I'm trying to refactor some matrix manipulation code for a speed boost. In particular, right now my function looks like this:
template<int SIZE> void do_something(matrix A) {
for (int i = 0; i < SIZE; ++i) {
// do something on column i of A
}
}
I saw some techniques that use templates to rewrite this as
#define SIZE whatever
template<int COL> void process_column(matrix A) {
// do something on column COL of A
process_column<COL + 1>(A);
}
template<> void process_column<SIZE - 1>(matrix A) {
return;
}
void do_something(matrix A) {
process_column<0>(A);
}
When I did that to my function and set compiler flags to inline appropriately, I saw a pretty decent (~10%) speed boost. But the problem is that SIZE is #defined not a template parameter and I will definitely be using different sizes in my program. So I want something like
template<int COL, int SIZE> void process_column(matrix A) {
// do something on column COL of A
process_column<COL + 1, SIZE>(A);
}
/* HOW DO I DECLARE THE SPECIFIC INSTANCE????
The compiler rightfully complained when I tried this: */
template<int SIZE> void process_column<SIZE - 1, SIZE>(matrix A) {
return;
}
template<int SIZE> void do_something(matrix A) {
process_column<0, SIZE>(A);
}
How do I declare the specific instance to get the loop to terminate? Thanks in advance!