R.2: In interfaces, use raw pointers to denote individual objects (only)

R.2: 只在接口中表示单独对象使用原始指针

Reason(原因)

Arrays are best represented by a container type (e.g., vector (owning)) or a span (non-owning). Such containers and views hold sufficient information to do range checking.

数组最好用容器类型(例如,vector(具有所有权))或者span(不包含所有权)表示。容器或span包含可以用于范围检查的信息。

Example, bad(反面示例)

void f(int* p, int n)   // n is the number of elements in p[]
{// ...p[2] = 7;   // bad: subscript raw pointer// ...
}

The compiler does not read comments, and without reading other code you do not know whether p really points to n elements. Use a span instead.

编译器不会读注释行,如果不看其他代码你无法知道p实际上指向n个元素。使用span吧。

Example(示例)

void g(int* p, int fmt)   // print *p using format #fmt
{// ... uses *p and p[0] only ...
}

Exception(例外)

C-style strings are passed as single pointers to a zero-terminated sequence of characters. Use zstring rather than char* to indicate that you rely on that convention.

C风格字符串作为指向以0结尾的字符序列的指针传递。使用zstring而不是char*以表明你遵守这个习惯。

Note(注意)

Many current uses of pointers to a single element could be references. However, where nullptr is a possible value, a reference may not be a reasonable alternative.

很多目前指向单独要素的指针可以使用引用。然而,当nullptr也是有效值时引用就不是一个合理的选择。

Enforcement(实施建议)

  • Flag pointer arithmetic (including ++) on a pointer that is not part of a container, view, or iterator. This rule would generate a huge number of false positives if applied to an older code base.

  • 如果一个指针不是来自容器,view或者迭代器并存在指针运算(包括++),进行提示。这条准则如果运用于旧代码会产生大量的假阳性结果(结果有问题但实际上没有问题,译者注)。

  • Flag array names passed as simple pointers

  • 提示用原始指针传递数组的情况。

原文链接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r2-in-interfaces-use-raw-pointers-to-denote-individual-objects-only


觉得本文有帮助?欢迎点赞并分享给更多的人。

更新文章,请关注微信公众号【面向对象思考】

C++核心准则R.2: 只在接口中表示单独对象使用原始指针​相关推荐

  1. C++核心准则边译边学-I.27 考虑使用指向实现的指针技术获得稳定的ABI

    I.27: For stable library ABI, consider the Pimpl idiom(考虑使用指向实现的指针技术获得稳定的ABI) Reason(原因) Because pri ...

  2. C++核心准则R.33: 表达函数会重置widget时,使用unique_ptr(widget)​作参数

    R.33: Take a unique_ptr<widget>& parameter to express that a function reseats the widget R ...

  3. C++核心准则R.32: 通过​unique_ptr(widget)类型参数表示函数试图获取widget的所有权

    R.32: Take a unique_ptr<widget> parameter to express that a function assumes ownership of a wi ...

  4. python函数赋值给对象_【Python核心编程笔记】一、Python中一切皆对象

    Python中一切皆对象本章节首先对比静态语言以及动态语言,然后介绍 python 中最底层也是面向对象最重要的几个概念-object.type和class之间的关系,以此来引出在python如何做到 ...

  5. C++核心准则边译边学-I.7 说明后置条件

    I.7: State postconditions (说明后置条件) Reason(原因) To detect misunderstandings about the result and possi ...

  6. php 语法 条件变量,C ++核心准则:注意条件变量的陷阱

    今天,我写了一篇关于条件变量的恐怖文章.您应该意识到条件变量的这一问题.C ++核心准则CP 42仅声明:"不要无条件等待". 等待!条件变量支持一个非常简单的概念.一个线程准备一 ...

  7. modbus软件开发实战指南_C++核心准则?GSL:指南支持库

    GSL: Guidelines support library GSL:指南支持库 The GSL is a small library of facilities designed to suppo ...

  8. 开源压缩算法brotli_Google的Brotli压缩算法,C ++核心准则以及更多新闻

    开源压缩算法brotli 在本周的开源新闻综述中,我们将介绍Google的Brotli压缩算法,适用于GitHub的Classroom,C ++ Core Guidelines等! 2015年9月20 ...

  9. 谷歌浏览器F12抓包如何过滤只显示接口请求不显示图片、js那些请求

    谷歌浏览器F12抓包如何过滤只显示接口请求不显示图片.js那些请求

最新文章

  1. 汉字验证码和算式验证码
  2. 再谈CVE-2017-7047 Triple_Fetch和iOS 10.3.2沙盒逃逸
  3. 如何“主动出击”提升网站的收录速度?
  4. ArrayBlockingQueue队列
  5. 【黑马程序员 C++教程从0到1入门编程】【笔记2】通讯录管理系统
  6. python的xlwt模块的常用方法
  7. Android 之PackageManager框架
  8. python编程实例-python编程实战
  9. 【转】搞清楚脚本中这些函数的调用规律
  10. Ajax---菜鸟教程
  11. word中px,pt,dpi,in,em,twip,emu之间的关系
  12. python flask后台框架_利用python实现后端写网页(flask框架)
  13. 三位数的茎叶图怎么看_三位数的茎叶图
  14. 推荐 10 个实用但偏执的 Java 编程技巧
  15. 蚂蚁金服面试经历(内含大量干货)
  16. JavaScript 一些小妙用
  17. web渗透--ftp暴力破解,明文、嗅探安全等
  18. qt_cannot find -lxxx, 链接动态/静态库失败
  19. 新品国产C2000,独立双核32位CPU,主频高达400MHz,QX320F280049
  20. SYN010E型全国产铷原子钟

热门文章

  1. char a[](字符串数组)和char *a(字符串指针)区别
  2. 一次简单的防接口攻击
  3. 数控星三角降压启动器接线图
  4. 水果套袋的分类有哪些?
  5. SystemVerilog学习笔记
  6. [HTML/CSS基础]知识点汇总
  7. ABP框架初始化数据(自定义)
  8. LEC对ASIC的重要性
  9. 第二代计算机网络的贡献,在第二代计算机上,有操作系统支持其运行。
  10. 电力通讯网络安全分区隔离配置——科东StoneWall-2000网络安全隔离设备(正,反向) 隔离内外网络