0

I have a template class for compile-time generation of a CRC table.

// crcTable.h
template <typename T, T polynomial, bool reversed>
struct CrcTable
{
    //...
}

I want an instance of this class as static constexpr member of my CRC16 class

// crc16.h
template<uint16_t polynomial = 0x1021, uint16_t initialValue = 0xFFFF>
class Crc16
{
public:
    //...
private:
    static constexpr CrcTable<uint16_t, polynomial, false> crcTable = CrcTable<uint16_t, polynomial, false>{};
}

As this is a static member I need to define it in a source file

// crc16.cpp
template <uint16_t polynomial, uint16_t initialValue>
constexpr CrcTable<uint16_t, polynomial, false> Crc16<polynomial, initialValue>::crcTable;

Compiling this gives me the following error:

undefined reference to `Crc16<(unsigned short)4129, (unsigned short)65535>::crcTable' clang: error: linker command failed with exit code 1 (use -v to see invocation)

Hint: if I define with inline in the header file it compiles and links successfully.

// crc16.h
template <uint16_t polynomial, uint16_t initialValue>
inline constexpr CrcTable<uint16_t, polynomial, false> Crc16<polynomial, initialValue>::crcTable;

What am I doing wrong? Is this even possible with templates what I am trying to do?

Elias
  • 45
  • 6

0 Answers0