R.36: Take a const shared_ptr<widget>& parameter to express that it might retain a reference count to the object ???/<widget>
R.36: 使用const shared_ptr<widget>&類型參數表示可能持有一個對象的引用計數。/<widget>
Reason(原因)
This makes the function's ??? explicit.
這樣可以使函數的行為更明確。
Example, good(範例)
<code>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/<widget>/<widget>/<widget>/<code>
Enforcement(實施建議)
- (Simple) Warn if a function takes a Shared_pointer
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
參數,卻沒有在任何一條代碼路徑上對它賦值或者調用reset(),提出警告並建議改用T* 或者 T& 代替。 - (Simple) ((Foundation)) Warn if a function takes a Shared_pointer
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& 代替。 - (Simple) ((Foundation)) Warn if a function takes a Shared_pointer
by rvalue reference. Suggesting taking it by value instead. - (簡單)((基本))如果一個函數以右值引用方式使用了Shared_pointer
參數,建議改用傳值方式。
原文鏈接
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-
覺得本文有幫助?請分享給更多人。
關注微信公眾號【面向對象思考】輕鬆學習每一天!
面向對象開發,面向對象思考!
閱讀更多 面向對象思考 的文章