<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++核心準(zhǔn)則ES.79:使用default處理一般case

          abc45b77a6a4fa123f8d83a3479acca2.webp

          ES.79: Use?default?to handle common cases (only)

          ES.79:使用default處理一般case


          Reason(原因)

          Code clarity. Improved opportunities for error detection.

          代碼清晰性。增加發(fā)現(xiàn)錯(cuò)誤的機(jī)會(huì)。


          Example(示例)

          enum E { a, b, c , d };

          void f1(E x)
          {
          switch (x) {
          case a:
          do_something();
          break;
          case b:
          do_something_else();
          break;
          default:
          take_the_default_action();
          break;
          }
          }

          Here it is clear that there is a default action and that cases?a?and?b?are special.

          可以清晰地看出存在一個(gè)默認(rèn)case,而a和b是特殊case。


          Example(示例)

          But what if there is no default action and you mean to handle only specific cases? In that case, have an empty default or else it is impossible to know if you meant to handle all cases:

          如果就是沒有默認(rèn)動(dòng)作,你只想處理特殊case時(shí)應(yīng)該怎么做呢?這種情況下,保留一個(gè)空的默認(rèn)處理,否則不可能知道你是否意圖處理所有case。

          void f2(E x)
          {
          switch (x) {
          case a:
          do_something();
          break;
          case b:
          do_something_else();
          break;
          default:
          // do nothing for the rest of the cases
          break;
          }
          }

          If you leave out the?default, a maintainer and/or a compiler may reasonably assume that you intended to handle all cases:

          如果漏掉了default,維護(hù)者或者編譯器可能會(huì)合情合理的假設(shè)你意圖處理所有case。

          void f2(E x)
          {
          switch (x) {
          case a:
          do_something();
          break;
          case b:
          case c:
          do_something_else();
          break;
          }
          }

          Did you forget case?d?or deliberately leave it out? Forgetting a case typically happens when a case is added to an enumeration and the person doing so fails to add it to every switch over the enumerators.

          你是忘記了case d還是故意遺漏的?忘記一個(gè)case通常發(fā)生在增加枚舉值之后卻沒有為所有switch語句增加針對(duì)該值的處理的時(shí)候。


          Enforcement(實(shí)施建議)

          Flag?switch-statements over an enumeration that don't handle all enumerators and do not have a?default. This may yield too many false positives in some code bases; if so, flag only?switches that handle most but not all cases (that was the strategy of the very first C++ compiler).

          標(biāo)記針對(duì)枚舉類型的、沒有處理所有枚舉值并且不包含default處理的switch語句。對(duì)于某些代碼這種做法可能會(huì)產(chǎn)生太多的假陽性;如果發(fā)生這種情況,只標(biāo)記處理了大部分case但不是全部case的情況(這正是很早期的C++編譯器采用的策略)。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es79-use-default-to-handle-common-cases-only




          覺得本文有幫助?請(qǐng)分享給更多人。

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

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


          瀏覽 32
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          <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>
                  欧美人妻中文字幕久久久苍井空 | 一级A片一区二区 | 亚洲综合另类 | 波多野结衣操逼视频 | 日韩性爱一级片 |