Skip to main content
added 130 characters in body
Source Link

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.

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;
}

/* never called, but needed to satisfy type checker! */
table compute(...) { return table{}; }

template<typename ...T, typename = bool[sizeof...(T) <= 256]>
constexpr table 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.

C++0x with constexpr. Works on GCC4.6.1

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 table {
  unsigned long data[256];
};

template<bool> struct 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>
constexpr typename sfinae<sizeof...(T) <= 255>::type compute(int n, T... t) {
  return compute(n+1, t..., computek(n));
}

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.

added 90 characters in body
Source Link

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;
}

/* never called, but needed to satisfy type checker! */
table compute(...) { return table{}; }

template<typename ...T, typename = bool[sizeof...(T) <= 256]>
constexpr table 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.

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;
}

/* never called, but needed to satisfy type checker! */
table compute(...) { return table{}; }

template<typename ...T, typename = bool[sizeof...(T) <= 256]>
constexpr table 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;
}

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;
}

/* never called, but needed to satisfy type checker! */
table compute(...) { return table{}; }

template<typename ...T, typename = bool[sizeof...(T) <= 256]>
constexpr table 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.

Source Link

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;
}

/* never called, but needed to satisfy type checker! */
table compute(...) { return table{}; }

template<typename ...T, typename = bool[sizeof...(T) <= 256]>
constexpr table 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;
}