<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.22: 使用make_shared()構(gòu)建共享shared_ptr

          9fc719da0bde2b701e160d66264e671c.webp

          R.22: Use make_shared() to make shared_ptrs

          R.22: 使用make_shared()構(gòu)建共享shared_ptr


          Reason(原因)

          If you first make an object and then give it to a shared_ptr constructor, you (most likely) do one more allocation (and later deallocation) than if you use make_shared() because the reference counts must be allocated separately from the object.

          如果你首先構(gòu)建一個對象然后將它交給shared_ptr的構(gòu)造函數(shù),和使用make_shared的情況相比,你(很有可能)多執(zhí)行了一次分配動作(和將要發(fā)生的一次釋放動作)。因為參照計數(shù)(此處應(yīng)該是shared_ptr對象,譯者注)的分配必須和對象的分配分別進行。


          Example(示例)

          Consider(考慮下面的代碼):


          shared_ptr p1 { new X{2} }; // bad
          auto p = make_shared(2); // good

          The make_shared() version mentions X only once, so it is usually shorter (as well as faster) than the version with the explicit new.

          make_shared方式只是用一次X對象,因此它通常比顯式調(diào)用new的代碼更簡短(執(zhí)行也更高效)。


          Enforcement(實施建議)

          (Simple) Warn if a shared_ptr is constructed from the result of new rather than make_shared.

          (簡單)如果使用new的結(jié)果構(gòu)造shared_ptr而不是make_shared,報警。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r22-use-make_shared-to-make-shared_ptrs



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

          關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學習每一天!

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


          ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

          ?在看 ?

          ? ? ? ? ? ? ? ? ?


          瀏覽 63
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          <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>
                  久久久精品电影 | 亚洲最新在线视频 | 日韩无码久久电影 | 经典一区二区 | 日日操夜夜叫狠狠插 |