I'm using the following template to encode unsigned char values:
template <unsigned char val>
struct Cell {
enum { value = val };
using add = Cell<val + 1>;
using sub = Cell<val - 1>;
};
I expected sub to behave like a standard unsigned char variable regarding overflow:
unsigned char x = 0;
x - 1; // 255
But instead I get a compiler error in Clang:
using cell = Cell<0>;
cell::sub::value; // Error here.
Non-type template argument evaluates to -1, which cannot be narrowed to type 'unsigned char'
Is overflow handled differently in template contexts?