搜索C风格的显式类型转换:a =(int)b; [英] Search of explicit type conversions in C style: a = (int)b;

查看:108
本文介绍了搜索C风格的显式类型转换:a =(int)b;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这种诊断的益处有疑问.
一位用户建议我们实施所有显式类型的搜索
在PVS-Studio分析仪中以C样式进行转换.
即是一种诊断程序,用于检测此类构造:

  int  * x =( int  *)y;
 float  a =  float (b);
浮动 c =(浮动)(d); 



其目的是将所有这些转换替换为更安全的
版本-reinterpret_cast/static_cast/const_cast.在此过程中
这样的重构,很可能会检测到代码中的某些缺陷.

当然,这不是检测关键错误,如果我们
实施此诊断,它将在[客户的
"部分中 特定请求],默认情况下处于禁用状态.

但是我什至怀疑这种诊断的好处.所以我决定问
其他用户:是否有人需要此选项来搜索显式
C风格的类型转换?任何人都想表演这种

解决方案

这可能很有用,因为它们实际上是代码中的危险"区域.
我的意思是,我认为修改C强制类型转换始终是一个好习惯.


我同意Carlo的观点,如果您是从C转向C ++,则绝对应该这样做.旧的C样式强制转换容易引起无法解释的错误,在运行的应用程序中很难发现. c2>(用于不相关的类型).

如果您可以正确地猜测它们是什么,则逐个案例,将其替换为C ++正确的强制类型转换会使代码更健壮.

我看到的唯一危险是,您无法以某种方式猜测应该使用哪种类型的转换((A*)&b会发生这种情况,其中bB实例,而AB都是模板参数C的转换将取决于AB实际上是实例化的,并且不明显-if static_cast对于AB作为相关类型起作用-在A中,reinterpret_cast A和B无关的情况必须视为错误.

换句话说,在某些旧代码中,C语言转换的较弱行为可能被有意识地用作功能"(不是错误!):在这种情况下,将其删除可能会更改正确(从计算理论上来说)代码.

当然,C ++社区会批评这样的代码,但是如果它存在并且被证明可以工作,您就可以使它不再起作用!


I have a question concerning the benefit of this diagnostic.
One user suggested that we implemented search of all the explicit type
conversions in C style in the PVS-Studio analyzer.
That is, a diagnostic to detect constructs of this kind:

int *x = (int *)y;
float a = float(b);
float c = (float)(d);



Its purpose is to replace all these conversions with their safer
versions - reinterpret_cast/static_cast/const_cast. During the process
of such refactoring, some defects in code may well be detected.

Of course, this is not detection of crucial errors, and if we
implement this diagnostic, it will be in the section [Customer''s
Specific Requests] and disabled by default.

But I even doubt the benefit of this diagnostic. So I decided to ask
other users: does anybody else need this option of searching for explicit
type conversions in C style? Would anybody like to perform this kind
of refactoring in their code?

解决方案

It might be useful, those are actually ''dangerous'' areas of the code.
I mean, revising C-like casts is always a good practice, in my opinion.


I agree with Carlo, you should definitely do this if you are moving from C to C++. The old C-style casts are prone to causing unexplained errors which can be very hard to discover in running applications.


C casts can be sometimes static_cast (for related types), sometime reinterpret_cast (for unrelated types).

If you can correctly guess what they are, case by case, replacing them with C++ proper casts makes the code more robust.

The only danger I see is where you cannot guess in a certain way, which cast you should use (it happens with (A*)&b, where b is a B instance and A and B are both template parameters. What the C cast will do depends on what A and B actually are on instantiation, and is not obvious that -if static_cast works for A and B as related types- the reinterpret_cast in case A & B are unrelated must be treated as error.

In other words, the weaker behavior of C cast, in certain old code, may had been consciously used as a "feature" (not error!): in such case, removing it, may alter the behavior of correct (In computation theory sense) code.

Of course, C++ community will criticize such a code, but if it exist and it is proved it works... you can make it no more working!


这篇关于搜索C风格的显式类型转换:a =(int)b;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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