日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++核心准则R.32: 通过​unique_ptr(widget)类型参数表示函数试图获取widget的所有权

發布時間:2023/12/14 c/c++ 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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: 通過unique_ptr<widget>類型參數表示函數試圖獲取widget的所有權

?

Reason(原因)

Using?unique_ptr?in this way both documents and enforces the function call's ownership transfer.

以這種方式使用unique_ptr可以從文檔和實現兩個方面強制進行所有權的移交。

?

Example(示例)

void sink(unique_ptr<widget>); // takes ownership of the widgetvoid uses(widget*); // just uses the widget

Example, bad(反面示例)

void thinko(const unique_ptr<widget>&); // usually not what you want

Enforcement(實施建議)

  • (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.

  • (簡單)如果一個函數通過左值引用方式使用了Unique_pointer<T>參數,卻沒有對它賦值,或者在至少一條代碼路徑上調用了reset,報警。建議使用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.

  • (簡單)((基本))?如果一個函數以常量引用方式使用Unique_pointer<T>參數,建議使用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

?


?

覺得本文有幫助?歡迎點贊并分享給更多的人。

更新文章,請關注微信公眾號【面向對象思考】

總結

以上是生活随笔為你收集整理的C++核心准则R.32: 通过​unique_ptr(widget)类型参数表示函数试图获取widget的所有权的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。