c++ - 如何修复隐式转换警告?

标签 c++ c++11 implicit-conversion unsigned signed

我正在阅读《C++ 如何编程》第 9 版,尝试学习 C++,并尝试使用第 7 章中的骑士问题进行练习。

根据输出中的“64”,代码显然存在某种逻辑错误(我将自行修复),但在执行此操作之前,我一直在理解以及如何处理编译器警告。

我已将 board 声明为 unsigned int 的二维数组,因为它只能包含值 0-7。同样,出于同样的原因,我已将 for 循环中的变量声明为 unsigned int。

我在 stackoverflow 上读过有关此问题的回复,但它们很快就超出了我的理解范围。

#include <iostream>
#include <array>
#include <iomanip>

using namespace std;

void printArray(const array< array< unsigned int, 8 >, 8 >);

int main() {
  array< array< unsigned int, 8 >, 8 > board = {};
  array< int, 8 > horizontal = {{ 2, 1, -1, -2, -2, -1, 1, 2 }};
  array< int, 8 > vertical = {{ -1, -2, -2, -1, 1, 2, 2, 1 }};
  unsigned int currentRow = 0;
  unsigned int currentCol = 0;
  for (unsigned int i = 0; i < 64; i++) {
    board[currentRow][currentCol] = i + 1;
    for (unsigned int j = 0; j < 8; j++) {
      if (currentRow + horizontal[j] >= 0 &&
        currentRow + horizontal[j] < 8 &&
        currentCol + vertical[j] >= 0 &&
        currentCol + vertical[j] < 8 &&
        board[currentRow + horizontal[j]][currentCol + vertical[j]] == 0
      ) {
        currentRow = currentRow + horizontal[j];
        currentCol = currentCol + vertical[j];
        break;
      }
    }
  }
  printArray(board);
}

void printArray(const array< array< unsigned int, 8 >, 8 > myBoard) {
  for ( unsigned int i = 0; i < 8; i++) {
    for ( unsigned int j = 0; j < 8; j++) {
      cout << '[' << setw(2) << setfill('0') << myBoard[i][j] << "] ";
    }
    cout << endl;
  }
}

输出:

~/Programming/cpp/knight => make
rm -fr build/*
clang++ -std=c++11 -stdlib=libc++ -Weverything -Wno-c++98-compat src/main.cpp -o build/main -Isrc/
src/main.cpp:18:24: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
      if (currentRow + horizontal[j] >= 0 &&
                     ~ ^~~~~~~~~~~~~
src/main.cpp:18:38: warning: comparison of unsigned expression >= 0 is always true [-Wtautological-compare]
      if (currentRow + horizontal[j] >= 0 &&
          ~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~
src/main.cpp:19:22: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
        currentRow + horizontal[j] < 8 &&
                   ~ ^~~~~~~~~~~~~
src/main.cpp:20:22: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
        currentCol + vertical[j] >= 0 &&
                   ~ ^~~~~~~~~~~
src/main.cpp:20:34: warning: comparison of unsigned expression >= 0 is always true [-Wtautological-compare]
        currentCol + vertical[j] >= 0 &&
        ~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~
src/main.cpp:21:22: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
        currentCol + vertical[j] < 8 &&
                   ~ ^~~~~~~~~~~
src/main.cpp:22:28: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
        board[currentRow + horizontal[j]][currentCol + vertical[j]] == 0
                         ~ ^~~~~~~~~~~~~
src/main.cpp:22:56: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
        board[currentRow + horizontal[j]][currentCol + vertical[j]] == 0
                                                     ~ ^~~~~~~~~~~
src/main.cpp:24:35: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
        currentRow = currentRow + horizontal[j];
                                ~ ^~~~~~~~~~~~~
src/main.cpp:25:35: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
        currentCol = currentCol + vertical[j];
                                ~ ^~~~~~~~~~~
10 warnings generated.
./build/main
[01] [24] [09] [00] [00] [26] [37] [00]
[10] [41] [02] [25] [36] [00] [00] [27]
[23] [08] [11] [40] [21] [38] [00] [00]
[64] [03] [22] [35] [00] [00] [28] [00]
[07] [12] [05] [20] [39] [34] [00] [00]
[04] [17] [14] [31] [00] [29] [00] [00]
[13] [06] [19] [16] [33] [00] [00] [00]
[18] [15] [32] [00] [30] [00] [00] [00]

最佳答案

编译器告诉你,当你编写时

currentRow + horizontal[j] >= 0

您要添加一个signed int 和一个unsigned int,因此signed int 会转换为unsigned int,因此总和的结果是一个 unsigned intunsigned int 始终大于或等于零。

所以这个测试总是true,这可能不完全是你想要的。

同样的问题

currentCol + vertical[j] >= 0

更一般地说,对于有符号/无符号值的每个总和。

一种选择是将 currentRowcurrentCol 定义为 (signed) int

否则,您可以在将unsigned值添加到signed值时适本地转换它们

int{currentRow} + horizontal[j] >= 0

关于c++ - 如何修复隐式转换警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48648942/

相关文章:

c++ - 从类到字符串的隐式和显式转换

c++ - 为什么显式运算符 bool 让我转换为任何原始类型?

c++ - 需要左值作为左操作数赋值时

c++ - 在 Visual Studio for C++ 中分析 dll 加载条件的最佳工具

c++ - std::bind 导致析构函数出现段错误

C++ lambda 捕获 this vs 通过引用捕获

c++ - 将 std::array 作为模板可变参数函数的参数传递

JavaScript 正在将字符串转换为字符串并导致错误

c++ - C++ 数组初始化的 Doxygen 问题

c++ - 一种独特的数据转换类型