Hi,
I'm trying to use expression templates to calculate values at compile time.
Doing it with just one parameter is no problem:
///Begin Listing 1
#include <iostream>
using namespace std;
template <int n> void UNROLL(){
cout << "n = " << n << endl;
UNROLL<n-1>();
}
template <> void UNROLL<0>() {
cout << "Hit Zero!" << endl;
}
int main(){
UNROLL<10>();
return 0;
}
///Begin Listing 1
However, if I want to use two parameters, I have to explicitly set both in
the abortion rule like this:
///Begin Listing 2
#include <iostream>
using namespace std;
template <int n, int m> void UNROLL(){
cout << "m*n = " << m*n << endl;
UNROLL<n, m-1>();
}
template <> void UNROLL<5, 0>() {
cout << "Hit Zero!" << endl;
}
int main(){
UNROLL<5, 10>();
return 0;
}
//End Listing 2
Of course, this is not what I want, I want the recursion to abort with
n == (any value) and m == 0. I tried to change the second program like this:
template <int n> void UNROLL<n, 0>() {
cout << "Hit Zero!" << endl;
}
But this gives me the error
temprektest_func.cpp:12: error: partial specialization `UNROLL<n, 0>' of
function template
and more errors about exceeding the maximum instantiation depth of
templates.
I tried google first. I did not find anything exactly like what I wanted,
but guessing from examples involving classes, this seemed to be the syntax
to use. Is this a limitation of my compiler (G++ 3.3.6), am I doing it
wrong or is it simply not possible to specialize expression templates in
this way?
Thanks, Martin