前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则ES.46:避免有损(窄化,截短)算数转换

C++核心准则ES.46:避免有损(窄化,截短)算数转换

作者头像
面向对象思考
发布2020-05-20 00:15:08
4290
发布2020-05-20 00:15:08
举报

ES.46: Avoid lossy (narrowing, truncating) arithmetic conversions

ES.46:避免有损(窄化,截短)算数转换

Reason(原因)

A narrowing conversion destroys information, often unexpectedly so.

窄化转换破坏信息,通常不是期待的动作。

Example, bad(反面示例)

A key example is basic narrowing:

主要的示例说明窄化的基本情况:

代码语言:javascript
复制
double d = 7.9;
int i = d;    // bad: narrowing: i becomes 7
i = (int) d;  // bad: we're going to claim this is still not explicit enough

void f(int x, long y, double d)
{
    char c1 = x;   // bad: narrowing
    char c2 = y;   // bad: narrowing
    char c3 = d;   // bad: narrowing
}
Note(注意)

The guidelines support library offers a narrow_cast operation for specifying that narrowing is acceptable and a narrow ("narrow if") that throws an exception if a narrowing would throw away information:

准则支持库提供了一个narrow_cast操作,可以用来表明窄化是可接受的;一个narrow(“如果发生窄化转换”)操作,它可以在丢失了任何信息时抛出异常。

代码语言:javascript
复制
i = narrow_cast<int>(d);   // OK (you asked for it): narrowing: i becomes 7
i = narrow<int>(d);        // OK: throws narrowing_error

We also include lossy arithmetic casts, such as from a negative floating point type to an unsigned integral type:

这两个操作也可以处理有损算数转换,例如从负浮点数转换为无符号整数的情况。

代码语言:javascript
复制
double d = -7.9;
unsigned u = 0;

u = d;                          // BAD
u = narrow_cast<unsigned>(d);   // OK (you asked for it): u becomes 4294967289
u = narrow<unsigned>(d);        // OK: throws narrowing_error
Enforcement(实施建议)

A good analyzer can detect all narrowing conversions. However, flagging all narrowing conversions will lead to a lot of false positives. Suggestions:

实现良好的代码分析器可以检出所有的窄化转换。但是标识所有的窄化转换会导致大量的假阳性结果。建议:

  • Flag all floating-point to integer conversions (maybe only float->char and double->int. Here be dragons! we need data).
  • 标记所有浮点数到整数的转换(或许只需要标记float到char和double到int。 都有可能! 我们需要数据)
  • Flag all long->char (I suspect int->char is very common. Here be dragons! we need data).
  • 标记所有long到char的转换(我怀疑int到char的转换很普遍。都有可能! 我们需要数据)
  • Consider narrowing conversions for function arguments especially suspect.
  • 函数参数的窄化转换尤其可疑。

链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es46-avoid-lossy-narrowing-truncating-arithmetic-conversions

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ES.46: Avoid lossy (narrowing, truncating) arithmetic conversions
  • Reason(原因)
    • Note(注意)
      • Enforcement(实施建议)
      相关产品与服务
      腾讯云代码分析
      腾讯云代码分析(内部代号CodeDog)是集众多代码分析工具的云原生、分布式、高性能的代码综合分析跟踪管理平台,其主要功能是持续跟踪分析代码,观测项目代码质量,支撑团队传承代码文化。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档