【问题标题】:Why do implicit conversions in function parameters of same size not throw a warning?为什么相同大小的函数参数中的隐式转换不会引发警告?
【发布时间】:2017-05-06 16:04:34
【问题描述】:

看那个例子:

//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 会放过这个?是否有检测这种隐式转换的选项?

干杯

【问题讨论】:

标签: c++ g++ implicit-conversion


【解决方案1】:

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

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

#

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

【讨论】:

    【解决方案2】:

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

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

    缺少的标志是:-Wsign-conversion

    【讨论】:

    • clang,有很好的-Weverything
    猜你喜欢
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多