常量表达式的计算结果为 -1,无法缩小为类型 'char' [-Wc++11-narrowing] 错误

constant expression evaluates to -1 which cannot be narrowed to type 'char' [-Wc++11-narrowing] error

我正在使用旧代码,在使用较新的 C++ 标准进行编译时,我在每个 -1 都遇到错误。

constant expression evaluates to -1 which cannot be narrowed to type 'char' [-Wc++11-narrowing]

这是代码片段

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}
};

我尝试将代码和括号更改为 之后的括号,但我仍然遇到相同的错误。有没有不恢复到旧 C++ 标准的解决方案?

要覆盖编译器,你必须写static_cast<char>(-1)

更新的 C++ 标准不再接受某些事情,因此您必须找到一种更现代的方式来做事情...