Это не особенно элегантно, но:
template <unsigned int N>
struct best_integer_type {
typedef best_integer_type<N-1>::type type;
};
template <>
struct best_integer_type<0> {
typedef char type;
};
template <>
struct best_integer_type<8> {
typedef short type;
};
template <>
struct best_integer_type<16> {
typedef int type;
};
template <>
struct best_integer_type<32> {
// leave out "type" entirely, to provoke errors
};
Тогда в вашем классе:
typename best_integer_type<SIZE>::type _value;
Он не имеет дело с отрицательными числами для SIZE
. Как и ваш исходный код, но в текстовом описании говорится, что нужно использовать char
, если SIZE <= 7
. Я ожидаю, что 0 <= SIZE <= 7
сделает.