R.36: Take a const shared_ptr<widget>& parameter to express that it might retain a reference count to the object ???

R.36: 使用const shared_ptr<widget>&类型参数表示可能持有一个对象的引用计数。

 

 

Reason(原因)

This makes the function's ??? explicit.

这样可以使函数的行为更明确。

 

Example, good(范例)

void share(shared_ptr<widget>);            // share -- "will" retain refcount

void reseat(shared_ptr<widget>&); // "might" reseat ptr

void may_share(const shared_ptr<widget>&); // "might" retain refcount

 

Enforcement(实施建议)

  • (Simple) Warn if a function takes a Shared_pointer<T> parameter by lvalue reference and does not either assign to it or call reset() on it on at least one code path. Suggest taking a T* or T& instead.
  • (简单)如果一个函数以左值引用方式使用了Shared_pointer<T>参数,却没有在任何一条代码路径上对它赋值或者调用reset(),提出警告并建议改用T* 或者 T& 代替。
  • (Simple) ((Foundation)) Warn if a function takes a Shared_pointer<T> by value or by reference to const and does not copy or move it to another Shared_pointer on at least one code path. Suggest taking a T* or T& instead.
  • (简单)((基本))如果一个函数以传值或者常量引用方式使用了Shared_pointer<T>参数,却没有在任何一条代码路径上拷贝它或者移动它,提出警告并建议改用T* 或者 T& 代替。
  • (Simple) ((Foundation)) Warn if a function takes a Shared_pointer<T> by rvalue reference. Suggesting taking it by value instead.
  • (简单)((基本))如果一个函数以右值引用方式使用了Shared_pointer<T>参数,建议改用传值方式。

 

原文链接

​https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r36-take-a-const-shared_ptrwidget-parameter-to-express-that-it-might-retain-a-reference-count-to-the-object-​

 


 

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

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