<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C++核心準則R.31:如果需要實現(xiàn)標準庫以外的智能指針,遵照標準庫...

          共 2087字,需瀏覽 5分鐘

           ·

          2020-04-08 23:24

          1b2bd794719d9cdbc61196661f138f4d.webp

          R.31: If you have non-std?smart pointers, follow the basic pattern from?std

          R.31:如果需要實現(xiàn)標準庫以外的智能指針,遵照標準庫中的基本模式


          Reason(原因)

          The rules in the following section also work for other kinds of third-party and custom smart pointers and are very useful for diagnosing common smart pointer errors that cause performance and correctness problems. You want the rules to work on all the smart pointers you use.

          后面章節(jié)中的準則也適用于其他類型的第三方和自定義的智能指針,它們對于發(fā)現(xiàn)一般的可能導致性能和正確性問題的智能指針錯誤很有效。你需要的是對所有智能指針都有效的的準則。

          Any type (including primary template or specialization) that overloads unary?*?and?->?is considered a smart pointer:

          重載了一元*和->的任何類型(包括主要的模板和特化)都可以看作智能指針。

          • If it is copyable, it is recognized as a reference counted?shared_ptr.

          • 如果它是可拷貝的,就被認為是帶有參照計數(shù)的共享指針。

          • If it is not copyable, it is recognized as a unique?unique_ptr.

          • 如果它不可拷貝,就被認為是獨占的unique_ptr。



          Example(示例)

          // use Boost's intrusive_ptr
          #include
          void f(boost::intrusive_ptr p) // error under rule 'sharedptrparam'
          {
          p->foo();
          }

          // use Microsoft's CComPtr
          #include
          void f(CComPtr p) // error under rule 'sharedptrparam'
          {
          p->foo();
          }

          Both cases are an error under the?sharedptrparam?guideline:?p?is a?Shared_pointer, but nothing about its sharedness is used here and passing it by value is a silent pessimization; these functions should accept a smart pointer only if they need to participate in the widget's lifetime management. Otherwise they should accept a?widget*, if it can be?nullptr. Otherwise, and ideally, the function should accept a?widget&. These smart pointers match the?Shared_pointer?concept, so these guideline enforcement rules work on them out of the box and expose this common pessimization.

          兩種情況都犯了sharedptrParam準則指出的錯誤:p是一個共享指針,但是這里沒有用到任何有關共享的功能。而且通過傳值方式傳遞智能指針是一種默認的許可;這個函數(shù)應該只在參與widget的生命周期管理時才接受智能指針。其他情況下:如果函數(shù)允許為空,它們應該接受widget*,否則應該接受widget&。這些智能指針匹配Shared_pointer概念,因此推薦這些準則推薦的規(guī)則也可以馬上適用于它們。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r31-if-you-have-non-std-smart-pointers-follow-the-basic-pattern-from-std




          覺得本文有幫助?請分享給更多人。

          關注微信公眾號【面向對象思考】輕松學習每一天!

          面向對象開發(fā),面向對象思考!



          瀏覽 45
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  黄色视频网站一级片 | 蜜桃传媒-熊猫成人网 | 99国产精品一区二区 | 国产一级二级三级在线观看 | 黑人操逼大片 |