小编Vin*_* Sa的帖子

为什么没有clang警告从double到int的隐式转换,但是从long到int时呢?

在以下代码中:

#include <iostream>
int main()
{
  const long l = 4294967296;
  int i = l;
  return i; //just to silence the compiler
}
Run Code Online (Sandbox Code Playgroud)

编译器警告隐式转换(使用-Wall和-std = c ++ 14)如下:

warning: implicit conversion from 'const long' to 'int' changes value from 4294967296 to 0 [-Wconstant-conversion]
Run Code Online (Sandbox Code Playgroud)

没关系.但是如果转换是从double到int,则没有警告,如下面的代码所示:

#include <iostream>
int main()
{
  const double d = 4294967296.0;
  int i = d;
  return i; //just to silence the compiler
}
Run Code Online (Sandbox Code Playgroud)

为什么编译器在这些情况下会有不同的反应?

注1:clang版本为3.6.2-svn240577-1~exp1

注2:我已经使用Compiler Explorer(gcc.godbolt.org)测试了许多其他版本的gcc,clang和icc.因此,所有测试版本的gcc(除了5.x)和icc都会发出警告.没有clang版本做到了.

c++ gcc clang gcc-warning c++11

7
推荐指数
2
解决办法
1827
查看次数

标签 统计

c++ ×1

c++11 ×1

clang ×1

gcc ×1

gcc-warning ×1