【发布时间】:2014-10-12 20:03:27
【问题描述】:
我正在使用以下模板对 unsigned char 值进行编码:
template <unsigned char val>
struct Cell {
enum { value = val };
using add = Cell<val + 1>;
using sub = Cell<val - 1>;
};
我希望 sub 在溢出方面表现得像标准的 unsigned char 变量:
unsigned char x = 0;
x - 1; // 255
但是我在 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'
在模板上下文中溢出的处理方式不同吗?
【问题讨论】:
标签: c++ templates template-meta-programming