<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++核心準則C.84:swap函數(shù)不應該失敗

          daa308a5db176801e9badfdec57398f1.webp

          釩鉛礦


          C.84: A swap function may not fail

          C.84:swap函數(shù)不應該失敗


          d92962aaab42542349668ccd11792a40.webp5c48c265ff50e2e1077730cb006d2a25.webp
          dc14be07096967365bb12e1cdf69a6ac.webp
          Reason(原因)
          dc14be07096967365bb12e1cdf69a6ac.webp
          5c48c265ff50e2e1077730cb006d2a25.webp

          swap is widely used in ways that are assumed never to fail and programs cannot easily be written to work correctly in the presence of a failing swap. The standard-library containers and algorithms will not work correctly if a swap of an element type fails.

          swap函數(shù)被廣泛地使用的方式就是假設它永遠不會失敗,而且也很難寫出即使swap出錯也能正常動作的程序。標準庫容器和算法在元素交換失敗時也無法正常工作。

          5f2cbcef0743bcc9c3e0a8fc3aa1bdcb.webp
          Example, bad(反面示例)
          5f2cbcef0743bcc9c3e0a8fc3aa1bdcb.webp
          void swap(My_vector& x, My_vector& y)
          {
          ? ?auto tmp = x; ? // copy elements
          ? ?x = y;
          ? ?y = tmp;
          }

          This is not just slow, but if a memory allocation occurs for the elements in tmp, this swap may throw and would make STL algorithms fail if used with them.

          這段代碼的問題不僅是慢,而且如果因為tmp的元素發(fā)生了內(nèi)存申請,如果使用它的話,這個swap可能拋出異常并令STL算法失敗。


          5c48c265ff50e2e1077730cb006d2a25.webp
          dc14be07096967365bb12e1cdf69a6ac.webp
          Enforcement(實施建議)
          dc14be07096967365bb12e1cdf69a6ac.webp
          5c48c265ff50e2e1077730cb006d2a25.webp

          (Simple) When a class has a swap member function, it should be declared noexcept.

          (簡單)如果類包含swap成員函數(shù),它應該被聲明為noexcept。


          5c48c265ff50e2e1077730cb006d2a25.webp
          dc14be07096967365bb12e1cdf69a6ac.webp
          原文鏈接
          dc14be07096967365bb12e1cdf69a6ac.webp
          5c48c265ff50e2e1077730cb006d2a25.webp

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c84-a-swap-function-may-not-fail




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

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

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


          瀏覽 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>
                  日韩AV家庭乱伦 | 五月天亚洲AV | 草逼伊人网站着的 | 人人妻人人摸 | 人人草视频在线 |