C++核心準(zhǔn)則R.33: 表達(dá)函數(shù)會(huì)重置widget時(shí),使用unique_ptr...

R.33: Take a?unique_ptr&?parameter to express that a function reseats the widget
R.33:? 表達(dá)函數(shù)會(huì)重置widget時(shí),使用unique_ptr
Reason(原因)
Using?unique_ptr?in this way both documents and enforces the function call's reseating semantics.
以這種方式使用unique_ptr可以從文檔和實(shí)現(xiàn)兩個(gè)方面強(qiáng)制函數(shù)調(diào)用的重置語義。
Note(注意)
"reseat" means "making a pointer or a smart pointer refer to a different object."
“重置”的意思是使指針或者智能指針參照另外一個(gè)對(duì)象。
Example(示例)
void reseat(unique_ptr&); // "will" or "might" reseat pointer Example, bad(反面示例)
void thinko(const unique_ptr&); // usually not what you want
Enforcement(實(shí)施建議)
(Simple) Warn if a function takes a?Unique_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. (簡(jiǎn)單)如果一個(gè)函數(shù)以左值引用方式使用了Unique_pointer
類型參數(shù),卻沒有至少一個(gè)代碼路徑上對(duì)它賦值或者調(diào)用reset方法,提出警告。建議改用T*或者T&?。 (Simple) ((Foundation)) Warn if a function takes a?Unique_pointer
?parameter by reference to?const. Suggest taking a?const T*?or?const T&?instead. (簡(jiǎn)單)((基礎(chǔ)))如果一個(gè)函數(shù)以常量引用形式使用了Unique_pointer
參數(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
覺得本文有幫助?請(qǐng)分享給更多人。
關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
