为什么相同大小的函数参数中的隐式转换不会引发警告?

Why do implicit conversions in function parameters of same size not throw a warning?

看那个例子:

//test.cpp
#include <iostream>
void test(unsigned int i, int j) {
  std::cout << i << " " << j << std::endl;
}
int main() {
  test(-1, -1);
  int x = -1;
  test(x,x);
  return 0;
}

与:

$ g++ -Wall -Wextra -Wpedantic test.cpp:
4294967295 -1
4294967295 -1

为什么 gcc 会放过这一点?是否有检测这种隐式转换的选项?

干杯

是的,我找到了。错误地认为 (-Wall -Wextra -W -Wpedantic -Wconversion) 会涵盖所有内容。但在

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

缺少的标志是:-Wsign-conversion

之前已经回答过了。原因之一是因为 C 允许它,而 c++ 意味着向后兼容。一些编译器会发出警告,尽管我在 gcc 5.2 上进行了测试,但它没有打开该警告的选项。

参见:Why does C++ allows implicit conversion from int to unsigned int?

#

刚刚从其他答案之一中发现您需要添加 -Wsign-conversion 标志。似乎 -Wall 应该这样做,但没有。