I'm currently trying to implement a small template which deduces the type needed to store a certain number of bits given as template parameter:
template<unsigned char BITS> class Register { public: unsigned long type; }; Furthermore I'm trying to specialize this template for certain bit sizes:
template<> class Register<8> { public: unsigned char type; }; template<> class Register<16> { public: unsigned short type; }; template<unsigned int N> Register<N+1>; Unfortunately this doesn't work as intended and fails to compile:
int _tmain(int argc, _TCHAR* argv[]) { Register<32>::type val32 = 0xDEADBEEF; assert(sizeof(val) == sizeof(unsigned long) ); Register<16>::valType val16 = 0xBEEF; assert(sizeof(val) == sizeof(unsigned short) ); Register<8>::valType val8 = 0xEF; assert(sizeof(val) == sizeof(unsigned char) ); Register<4>::valType val4 = 0xF; assert(sizeof(val) == sizeof(unsigned char) ); return 0; } Maybe someone can give me a pointer to some helpful text or tell me what's wrong with my approach?