前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则ES.102:使用有符号数进行数学运算

C++核心准则ES.102:使用有符号数进行数学运算

作者头像
面向对象思考
发布2020-06-17 19:07:56
4220
发布2020-06-17 19:07:56
举报

ES.102: Use signed types for arithmetic

ES.102:使用有符号数进行数学运算

Reason(原因)

Because most arithmetic is assumed to be signed; x - y yields a negative number when y > x except in the rare cases where you really want modulo arithmetic.

因为大部分数学运算都是有符号的。当x>y时,x-y会返回一个负数,极少情况实际需要的是按模运算。

Example(示例)

Unsigned arithmetic can yield surprising results if you are not expecting it. This is even more true for mixed signed and unsigned arithmetic.

如果不是你有意为之,无符号运算可能产生意外的结果。如果混用有符号数和无符号数,问题会更明显。

template<typename T, typename T2>
T subtract(T x, T2 y)
{
    return x - y;
}

void test()
{
    int s = 5;
    unsigned int us = 5;
    cout << subtract(s, 7) << '\n';       // -2
    cout << subtract(us, 7u) << '\n';     // 4294967294
    cout << subtract(s, 7u) << '\n';      // -2
    cout << subtract(us, 7) << '\n';      // 4294967294
    cout << subtract(s, us + 2) << '\n';  // -2
    cout << subtract(us, s + 2) << '\n';  // 4294967294
}

Here we have been very explicit about what's happening, but if you had seen us - (s + 2) or s += 2; ...; us - s, would you reliably have suspected that the result would print as 4294967294?

代码中我们已经很明确地知道发生了什么。但是如果你看到us - (s + 2) or s += 2; ...; us - s,你真的可以想象结果是4294967294么?

Exception(例外)

Use unsigned types if you really want modulo arithmetic - add comments as necessary noting the reliance on overflow behavior, as such code is going to be surprising for many programmers.

如果你真的需要按模运算-增加必要的注释提示对溢出行为的依赖,这样的代码会令很多程序员疑惑。

Example(示例)

The standard library uses unsigned types for subscripts. The built-in array uses signed types for subscripts. This makes surprises (and bugs) inevitable.

标准库使用无符号类型作为下标。内置数组使用有符号数作为下标。这会导致代码难于理解并不可避免地带来错误。

int a[10];
for (int i = 0; i < 10; ++i) a[i] = i;
vector<int> v(10);
// compares signed to unsigned; some compilers warn, but we should not
for (gsl::index i = 0; i < v.size(); ++i) v[i] = i;

int a2[-2];         // error: negative size

// OK, but the number of ints (4294967294) is so large that we should get an exception
vector<int> v2(-2);

Use gsl::index for subscripts; see ES.107.

使用ES.107中介绍的gsl::index作为下标。

Enforcement(实施建议)

  • Flag mixed signed and unsigned arithmetic
  • 标记有符号数和无符号数混用的数学运算。
  • Flag results of unsigned arithmetic assigned to or printed as signed.
  • 标记将无符号数学运算的结果赋值给有符号数或者作为有符号数print输出的情况。
  • Flag negative literals (e.g. -2) used as container subscripts.
  • 标记使用负值作为容器下标的情况。
  • (To avoid noise) Do not flag on a mixed signed/unsigned comparison where one of the arguments is sizeof or a call to container .size() and the other is ptrdiff_t.
  • (为了避免误判)当一个参数是sizeof或者container.size()的返回值,而另一个参数是ptrdiff_t的时候,不要标记有符号数/无符号数混合的比较操作。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es102-use-signed-types-for-arithmetic

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reason(原因)
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档