<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.21:不需要共享所有權(quán)時應該使用unique_ptr而不是?s...

          633890507744c33d13bb15644d0d3b99.webp

          R.21: Prefer?unique_ptr?over?shared_ptr?unless you need to share ownership

          R.21:不需要共享所有權(quán)時應該使用unique_ptr而不是shared_ptr


          Reason(原因)

          A?unique_ptr?is conceptually simpler and more predictable (you know when destruction happens) and faster (you don't implicitly maintain a use count).

          unique_ptr從概念上更簡單,動作更加可預見(你知道析構(gòu)動作什么時候發(fā)生)而且更快(不需要隱式維護使用計數(shù))。


          Example, bad(反面示例)

          This needlessly adds and maintains a reference count.

          不必要地增加和維護參照計數(shù)。

          void f()
          {
          shared_ptr base = make_shared();
          // use base locally, without copying it -- refcount never exceeds 1
          } // destroy base
          Example(示例)

          This is more efficient:

          下面的代碼更高效:

          void f()
          {
          unique_ptr base = make_unique();
          // use base locally
          } // destroy base

          Enforcement(實施建議)

          (Simple) Warn if a function uses a?Shared_pointer?with an object allocated within the function, but never returns the?Shared_pointer?or passes it to a function requiring a?Shared_pointer&. Suggest using?unique_ptr?instead.

          (簡單)如果函數(shù)使用shared_ptr管理其內(nèi)局部分配的對象,但是從來沒有返回該智能指針或者將其傳遞個一個需要shared_ptr&的函數(shù),發(fā)出警告。建議使用unique_ptr。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r21-prefer-unique_ptr-over-shared_ptr-unless-you-need-to-share-ownership



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

          關注【面向?qū)ο笏伎肌枯p松學習每一天!

          面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>

          瀏覽 100
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          <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>
                  人人摸人人搞人人操 | 国产视频aaa | 欧美日韩性爱电影网 | 婷婷丁香性爱 | 91做爱视频 |