【发布时间】:2021-01-22 13:46:17
【问题描述】:
我正在使用旧代码,并且在使用较新的 C++ 标准进行编译时,每个 -1 都会出错。
constant expression evaluates to -1 which cannot be narrowed to type 'char' [-Wc++11-narrowing]
这里是sn-p代码
typedef struct {
//short len;
//unsigned short cw;
char x, y, v, w;
} testStruct;
const testStruct testArr[] = {
{ 1, -1, 0, 0},
{ -1, 1, 0, 0},
{ 0, 0, -1, 1},
{ 0, 1, -1, 0},
{ 0, -1, 1, 0},
{ 0, 0, 1, -1},
{ 1, 1, 0, 0},
{ 0, 0, -1, -1},
{ -1, -1, 0, 0},
{ 0, -1, -1, 0},
{ 1, 0, -1, 0},
{ 0, 1, 0, -1},
{ -1, 0, 1, 0},
{ 0, 0, 1, 1},
{ 1, 0, 1, 0},
{ 0, -1, 0, 1},
{ 0, 1, 1, 0},
{ 0, 1, 0, 1},
{ -1, 0, -1, 0},
{ 1, 0, 0, 1},
{ -1, 0, 0, -1},
{ 1, 0, 0, -1},
{ -1, 0, 0, 1},
{ 0, -1, 0, -1}
};
我尝试将代码和括号更改为 warning: narrowing conversion C++11 后面的括号,但我仍然遇到同样的错误。有没有不回退到旧 C++ 标准的解决方案?
【问题讨论】:
-
你能把结构改成
signed char x, y, v, w;吗? -
与其他整数类型不同,
char是有符号还是无符号是未指定的。