为什么clang不警告关于从double到int的隐式转换,但是在从long到int的情况下这样做呢? [英] Why doesn't clang warn about implicit conversion from double to int, but do it when from long to int?

查看:139
本文介绍了为什么clang不警告关于从double到int的隐式转换,但是在从long到int的情况下这样做呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中:

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

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

the compiler warns about implicit conversion (using -Wall and -std=c++14) as following:

warning: implicit conversion from 'const long' to 'int' changes value from 4294967296 to 0 [-Wconstant-conversion]

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

which is ok. But there is no warning if the conversion is from double to int, as in the following code:

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

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

Why the compiler reacts differently in these situations?

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

Note 1: clang version is 3.6.2-svn240577-1~exp1

注2:感谢Compiler Explorer(gcc.godbolt.org),我已经用gcc,clang和icc的许多其他版本对其进行了测试.因此,所有经过测试的gcc版本(5.x除外)和icc均发出警告.没有clang版本.

Note 2: I've tested it with many others versions of gcc, clang and icc thanks to Compiler Explorer (gcc.godbolt.org). So all tested versions of gcc (with exception of 5.x) and icc threw the warning. No clang version did it.

推荐答案

好吧,请阅读

Well, by reading this great article named "What Every C Programmer Should Know About Undefined Behavior", specially part #3/3, at LLVM Project Blog, written by Chris Lattner - the main author of LLVM - I could understand better the Clang's Approach to Handling Undefined Behavior.

因此,为了确保您对优化和节省时间的强烈要求-最终性能"-

So, in order to guarantee your strong appeal for optimization and time economy - "ultimate performance" -

请记住,尽管编译器因没有动态而受到限制信息,并限于不消耗大量精力的情况下编译时间.

Keep in mind though that the compiler is limited by not having dynamic information and by being limited to what it can without burning lots of compile time.

默认情况下,Clang不会运行所有相关的未定义行为检查,

Clang doesn't run all related undefined behavior checks by default,

Clang为许多未定义行为类别生成警告(包括对null的取消引用,过大的班次等)在代码中显而易见,可以捕获一些常见错误.

Clang generates warnings for many classes of undefined behavior (including dereference of null, oversized shifts, etc) that are obvious in the code to catch some common mistakes.

Clang和LLVM提供了诸如 Clang静态分析器 Klee项目 -fcatch-undefined-behavior (现在 UndefinedBehaviorSanitizer - UBSan -)以避免这些可能的错误.

instead of this, Clang and LLVM provides tools like Clang Static Analyzer, Klee project, and the -fcatch-undefined-behavior (now UndefinedBehaviorSanitizer - UBSan - ) to avoid these possible bugs.

通过在呈现的代码 clang ++ 中运行带有以下参数 -fsanitize = undefined UBSan ,该错误将被捕获,如下所示:

By running UBSan in the presented code, clang++ with the following argument -fsanitize=undefined the bug will be catched as following:

运行时错误:值4.29497e + 09超出类型为'int'的可表示值的范围

这篇关于为什么clang不警告关于从double到int的隐式转换,但是在从long到int的情况下这样做呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆