g++ -Wuninitialized 在 std::min() 处不显示警告

问题描述

我使用 g++ -Wuninitialized(g++ 7.5.0 版)编译了以下代码,但没有收到任何警告:

#include <algorithm>

int main() {

    int a,b;

    b = std::min(a,0);

}

如您所见,std::min() 是使用参数 a0 调用的,但此时 a 尚未初始化。这应该给你一个警告,但它没有。

如果你用自定义函数替换 std::min(),编译器会给你一个警告:

代码

int f(int x) {
  
    return 2 * x;

}

int main() {

    int a,b;

    b = f(a);

}

警告

<source>: In function 'int main()':
<source>:11:10: warning: 'a' is used uninitialized in this function [-Wuninitialized]
   11 |     b = f(a);
      |         ~^~~

为什么这不适用于 std::min()?我也试过 std::max() 并且它也没有给我警告。但是使用 std::hypot() 我得到了一个。有没有办法让它也适用于 std::min()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...