Is there a way to pre-compute an array of values based on templates? In the following example I would like the 'powers_of_2' array to have 256 values computed at compile-time if that is possible without having to type all of the values.
#include <iostream>
using namespace std;
template <int X, char Y>
struct power {
enum { value = X * power<X,Y-1>::value };
};
template <int X>
struct power<X,1> {
enum { value = X };
};
template <int X>
struct power<X,0> {
enum { value = 1 };
};
int _tmain(int argc, _TCHAR* argv[])
{
int powers_of_2[] = { power<2,0>::value, power<2,1>::value, ..., power<2,255>::value };
cout << powers_of_2[1] << endl;
return 0;
}