F.20: For "out" output values, prefer return values to output parameters(输出结果时更应该使用返回值而不是输出参数)

Reason(原因)

A return value is self-documenting, whereas a & could be either in-out or out-only and is liable to be misused.返回值本身可以说明用途,而引用类型可以是输入/输出参数也有可能只是输出参数,容易被误用。

This includes large objects like standard containers that use implicit move operations for performance and to avoid explicit memory management.这种观点可以覆盖像标准容器那样的大对象,它们会为了性能和避免显式内存管理而使用隐式移动操作。

If you have multiple values to return, use a tuple or similar multi-member type.如果你有多个值需要返回,使用tuple或者类似的多成员类型。

译者注:tuple可以参考以下文章。

https://mp.weixin.qq.com/s/kQEYhjpfE9XYBiRgp_fJSw

Example(示例)

// OK: return pointers to elements with the value xvector find_all(const vector&, int x);// Bad: place pointers to elements with value x in-outvoid find_all(const vector&, vector& out, int x);

Note(注意)

A struct of many (individually cheap-to-move) elements may be in aggregate expensive to move.

包含多个(单独看都可以低成本移动)元素的结构体合起来移动时可能会代价高昂。

It is not recommended to return a const value. Such older advice is now obsolete; it does not add value, and it interferes with move semantics.

不推荐返回常量值。这种过时的建议现在已经被淘汰;它不会带来好处,而且其接口含有移动语义。

const vector fct(); // bad: that "const" is more trouble than it is worthvector g(const vector& vx){ // ... fct() = vx; // prevented by the "const" // ...  return fct(); // expensive copy: move semantics suppressed by the "const"}

The argument for adding const to a return value is that it prevents (very rare) accidental access to a temporary. The argument against is prevents (very frequent) use of move semantics.

建议为返回值增加const修饰的观点认为,这样会阻止(极少发生的)对临时变量的意外访问。相反的观点认为这样做会(非常多地)阻止移动语义的运用。

Exceptions(例外)

  • For non-value types, such as types in an inheritance hierarchy, return the object by unique_ptr or shared_ptr.对于非值类型函数,例如处于继承关系中的类型,通过unique_ptr或者shared_ptr返回对象。译者注:两种方式都可以避免不必要的拷贝动作。
  • If a type is expensive to move (e.g., array), consider allocating it on the free store and return a handle (e.g., unique_ptr), or passing it in a reference to non-const target object to fill (to be used as an out-parameter).如果某种类型(例如array)的移动成本很高,考虑从自由存储上为其申请内存并使用句柄(例如unique_prt)返回它,或者通过用于填充的非常量对象的引用来传递。译者注:POD是Plain old data structure的简称,是C++语言的标准中定义的一类数据结构,可以简单地理解只包含单纯数据类型的结构体。
  • To reuse an object that carries capacity (e.g., std::string, std::vector) across multiple calls to the function in an inner loop: treat it as an in/out parameter and pass by reference.为了让处于内循环中的函数调用可以重复使用带有容量的对象(例如std::string,std::vector):把它看做输入/输出参数并通过引用传递。

Example(示例)

struct Package { // exceptional case: expensive-to-move object char header[16]; char load[2024 - 16];};Package fill(); // Bad: large return valuevoid fill(Package&); // OKint val(); // OKvoid val(int&); // Bad: Is val reading its argument

译者注:示例代码说明的是POD使用引用传递输出值,而小数据者应该直接使用返回值。

Enforcement(实施建议)

  • Flag reference to non-const parameters that are not read before being written to and are a type that could be cheaply returned; they should be "out" return values.警告那些没有在写之前读(没有输入用途)而且可以低成本返回的参数,它们应该作为返回值输出。
  • Flag returning a const value. To fix: Remove const to return a non-const value instead.警告返回常数值的状况。修改方法:去掉常量修饰,返回一个非常量。

觉得本文有帮助?请分享给更多人。

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

面向对象设计,面向对象编程,面向对象思考!

使用说明 vector_C++核心准则编译边学-F.20 输出结果时应该使用返回值相关推荐

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

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

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

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

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

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

  4. C++核心准则ES.56​:只在需要将一个对象显式移动到另外的作用域时使用std::move​

    ES.56: Write std::move() only when you need to explicitly move an object to another scope ES.56:只在需要 ...

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

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

  6. c++ h cpp文件如何关联_C++核心准则SF.5: .cpp文件必须包含定义它接口的.h文件

    SF.5: A .cpp file must include the .h file(s) that defines its interface SF.5: .cpp文件必须包含定义它接口的.h文件 ...

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

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

  8. C++核心准则E.25:如果不能抛出异常,模仿RAII方式进行资源管理

    E.25: If you can't throw exceptions, simulate RAII for resource management E.25:如果不能抛出异常,模仿RAII方式进行资 ...

  9. Linux学习-额外(单一)核心模块编译

    编译前注意事项 由于我们的核心原本就有提供很多的核心工具给硬件开发商来使用, 而硬件开发商也需要针对核心 所提供的功能来设计他们的驱动程序模块,因此, 我们如果想要自行使用硬件开发商所提供的模块 来进 ...

最新文章

  1. CF982 C Cut 'em all!【树/DFS/思维】
  2. ArcGIS10新功能之制作地图集
  3. Py之SnowNLP:SnowNLP中文处理包的简介、安装、使用方法、代码实现之详细攻略
  4. mysql数据库dao模式_古诗MySQL数据库DAO模式实现
  5. 【小米校招笔试】一个数组是由有序数组经过n次循环移动后所得,请你用最快速度查找某个元素位置
  6. 深入信号和槽(Signals and Slots in Depth)
  7. 广东省2021高考2bi补录成绩查询,重磅!广东省2021年普通高考美术统考成绩1月8日起可查询!...
  8. 针对Mysql数据库服务器的优化
  9. Java实现的FTP协议断点续传功能(上传/下载)通用类
  10. html原生listview,Html中使用M$控件系列之 ListView 篇
  11. mui ajax的值php怎样获取,关于mui.ajax的设置,以及php取不到data值的问题的方法
  12. Redis过期策略及内存淘汰机制
  13. 软件工程基础知识--系统测试
  14. 模拟人生畅玩版正在连接至服务器,模拟人生™:畅玩版无法连接服务器如何解决...
  15. 《土豆荣耀》重构笔记(五)创建角色以及怪物的动画
  16. 北航计算机组成期末试题,北航-计算机组成题目汇总.pdf
  17. 1087: Time
  18. Xero 系列文章分享-Xero Vs MYOB
  19. 安卓启动中的PARTUUID
  20. Java基础Object类的equals方法

热门文章

  1. X86智能相机视觉系统解决方案
  2. 20155328 《信息安全系统设计基础》 课程总结
  3. 原码,反码,补码,移码的概念以及各自的用途和优点
  4. swift闭包 notes http://www.gittielabs.com
  5. 增量加载(Incremental Loads)
  6. 霍夫变换MATLAB怎么实现,做过Matlab关于霍夫变换检测圆的高手请进
  7. html弹窗赋值给查询框,bootstrap模态框动态赋值, ajax异步请求数据后给id为queryInfo的模态框赋值并弹出模态框(JS)...
  8. java int 0.5_java int转float精度缺失原因?
  9. python qthread 线程退出_线程:概念和实现
  10. print_r php encode,详细介绍PHP在调试时echo print() print_r() var_dump()的区别分享