为什么在相同大小的功能参数中隐式转换不会发出警告

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

本文关键字:转换 警告 参数 功能 为什么      更新时间:2023-10-16

请参阅该示例:

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

with:

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

为什么海湾合作杂志让那个滑倒?并且是否可以选择检测这种隐式转换?

欢呼

之前已经回答过。原因之一是因为C允许它,而C 则是向后兼容的。一些编译器会警告,尽管我在GCC 5.2上进行了测试,但它没有打开警告的选择。

请参阅:为什么C 允许从int到未签名的int?

的隐式转换#

刚从其他答案之一中找到您需要添加-wsign -conversion标志。似乎是-wall应该这样做,但不做。

是的,我找到了它。误以为(-wall -wextra -w -wpedantic -wconversion(将涵盖所有内容。但是在

https://gcc.gnu.org/onlinedocs/gcc/warning-options.html

缺失的标志为: -wsign-conversion