C++0x with constexpr
. Works on GCC4.6.1
struct table {
unsigned long data[256];
};
constexpr unsigned long computek(unsigned long c, int k = 0) {
return k < 8 ? computek((c & 1) ? (0xedb88320L ^ (c >> 1)) : (c >> 1), k+1) : c;
}
/*struct nevertable called,{
but neededunsigned tolong satisfydata[256];
};
template<bool> typestruct checker!sfinae */
{ typedef table type; };
template<> struct sfinae<false> { };
template<typename ...T>
constexpr typename sfinae<sizeof...(T) == 256>::type compute(int n, T... t) {
return table {{ t... }};
}
template<typename ...T,T>
constexpr typename = bool[sizeofsfinae<sizeof...(T) <= 256]>
constexpr table255>::type compute(int n, T... t) {
return n < 256 ? compute(n+1, t..., computek(n)) : table{{ t... }};
}
constexpr table crc_table = compute(0);
#include <iostream>
int main() {
for(int i = 0; i < 256; i++)
std::cout << crc_table.data[i] << std::endl;
}
You can then use crc_table.data[X]
at compile time because crc_table
is constexpr
.