前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则ES.107:不要使用无符号数下标,使用gsl::index更好​

C++核心准则ES.107:不要使用无符号数下标,使用gsl::index更好​

作者头像
面向对象思考
发布2020-06-24 10:41:46
8270
发布2020-06-24 10:41:46
举报

ES.107: Don't use unsigned for subscripts, prefer gsl::index

ES.107:不要使用无符号数下标,使用gsl::index更好

Reason(原因)

To avoid signed/unsigned confusion. To enable better optimization. To enable better error detection. To avoid the pitfalls with auto and int.

为了避免有符号数/无符号数混用带来的问题。有利实现更好的优化和错误检查。避免auto和int类型带来的陷阱。

Example, bad(反面示例)

代码语言:javascript
复制
vector<int> vec = /*...*/;

for (int i = 0; i < vec.size(); i += 2)                    // may not be big enough
    cout << vec[i] << '\n';
for (unsigned i = 0; i < vec.size(); i += 2)               // risk wraparound
    cout << vec[i] << '\n';
for (auto i = 0; i < vec.size(); i += 2)                   // may not be big enough
    cout << vec[i] << '\n';
for (vector<int>::size_type i = 0; i < vec.size(); i += 2) // verbose
    cout << vec[i] << '\n';
for (auto i = vec.size()-1; i >= 0; i -= 2)                // bug
    cout << vec[i] << '\n';
for (int i = vec.size()-1; i >= 0; i -= 2)                 // may not be big enough
    cout << vec[i] << '\n';
Example, good(范例)
代码语言:javascript
复制
vector<int> vec = /*...*/;

for (gsl::index i = 0; i < vec.size(); i += 2)             // ok
    cout << vec[i] << '\n';
for (gsl::index i = vec.size()-1; i >= 0; i -= 2)          // ok
    cout << vec[i] << '\n';
Note(注意)

The built-in array uses signed subscripts. The standard-library containers use unsigned subscripts. Thus, no perfect and fully compatible solution is possible (unless and until the standard-library containers change to use signed subscripts someday in the future). Given the known problems with unsigned and signed/unsigned mixtures, better stick to (signed) integers of a sufficient size, which is guaranteed by gsl::index.

内置数组使用有符号数下标。标准库容器使用无符号数下标。因此不存在完美、完全兼容的解决方案(除非将来某一天标准库容器转而使用有符号数下标)。考虑到使用无符号数或者有符号数/无符号数混合可能带来的问题,较好的选择是赋予(有符号)整数足够大的空间,这一点可以通过使用gsl::index保证。

Example(示例)

代码语言:javascript
复制
template<typename T>
struct My_container {
public:
    // ...
    T& operator[](gsl::index i);    // not unsigned
    // ...
};
Example(示例)
代码语言:javascript
复制
??? demonstrate improved code generation and potential for error detection ???
Alternatives(其他选项)

Alternatives for users

利用者角度的其他选项

  • use algorithms
  • 使用算法
  • use range-for
  • 使用范围for
  • use iterators/pointers
  • 使用指针和迭代器

Enforcement(实施建议)

  • Very tricky as long as the standard-library containers get it wrong.
  • 如果标准库容器出问题了,很难检出。
  • (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#es107-dont-use-unsigned-for-subscripts-prefer-gslindex

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

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

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

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

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