C++核心准则R.32: 通过unique_ptr(widget)类型参数表示函数试图获取widget的所有权
R.32: Take a?unique_ptr<widget>?parameter to express that a function assumes ownership of a?widget
R.32: 通過(guò)unique_ptr<widget>類(lèi)型參數(shù)表示函數(shù)試圖獲取widget的所有權(quán)
?
Reason(原因)
Using?unique_ptr?in this way both documents and enforces the function call's ownership transfer.
以這種方式使用unique_ptr可以從文檔和實(shí)現(xiàn)兩個(gè)方面強(qiáng)制進(jìn)行所有權(quán)的移交。
?
Example(示例)
void sink(unique_ptr<widget>); // takes ownership of the widgetvoid uses(widget*); // just uses the widgetExample, bad(反面示例)
void thinko(const unique_ptr<widget>&); // usually not what you wantEnforcement(實(shí)施建議)
-
(Simple) Warn if a function takes a?Unique_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.
-
(簡(jiǎn)單)如果一個(gè)函數(shù)通過(guò)左值引用方式使用了Unique_pointer<T>參數(shù),卻沒(méi)有對(duì)它賦值,或者在至少一條代碼路徑上調(diào)用了reset,報(bào)警。建議使用T*或者T&代替。
-
(Simple) ((Foundation)) Warn if a function takes a?Unique_pointer<T>?parameter by reference to?const. Suggest taking a?const T*?or?const T&?instead.
-
(簡(jiǎn)單)((基本))?如果一個(gè)函數(shù)以常量引用方式使用Unique_pointer<T>參數(shù),建議使用const T*或者const T&代替。
?
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r32-take-a-unique_ptrwidget-parameter-to-express-that-a-function-assumes-ownership-of-a-widget
?
?
覺(jué)得本文有幫助?歡迎點(diǎn)贊并分享給更多的人。
更新文章,請(qǐng)關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌?/p>
總結(jié)
以上是生活随笔為你收集整理的C++核心准则R.32: 通过unique_ptr(widget)类型参数表示函数试图获取widget的所有权的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 关于全球苹果手机的型号版本介绍
- 下一篇: 软件性能测试方案模板,性能测试方案模板