<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++核心準則ES.78:不要依靠switch語句的隱式下沉處理

          c25730243ff9413a3c6f15c6ef1003ad.webp

          ES.78: Don't rely on implicit fallthrough in?switch?statements

          ES.78:不要依靠switch語句的隱式下沉處理


          Reason(原因)

          Always end a non-empty?case?with a?break. Accidentally leaving out a?break?is a fairly common bug. A deliberate fallthrough can be a maintenance hazard and should be rare and explicit.

          通常情況下使用break中止一個非空case處理。意外漏掉某個break通常是一個錯誤。故意的下沉處理可能帶來維護風險,應該少用并明示用法。


          Example(示例)

          switch (eventType) {
          case Information:
          update_status_bar();
          break;
          case Warning:
          write_event_log();
          // Bad - implicit fallthrough
          case Error:
          display_error_window();
          break;
          }

          Multiple case labels of a single statement is OK:

          一個語句中包含多個標簽是沒有問題的。

          switch (x) {
          case 'a':
          case 'b':
          case 'f':
          do_something(x);
          break;
          }

          Return statements in a case label are also OK:?

          case標簽中使用返回語句也沒有問題:

          switch (x) { case 'a': ?  return?1;??case?'b':??  return?2;??case?'c':??  return?3;??}


          Exceptions(例外)

          In rare cases if fallthrough is deemed appropriate, be explicit and use the?[[fallthrough]]?annotation:

          在很少的情況下,如果確信下沉處理是合適的,可以使用[[fallthrougn]]記法明確標明。


          switch (eventType) {
          case Information:
          update_status_bar();
          break;
          case Warning:
          write_event_log();
          [[fallthrough]];
          case Error:
          display_error_window();
          break;
          }
          Note(注意)


          Enforcement(實施建議)

          Flag all implicit fallthroughs from non-empty?cases.

          標記所有來自非空case的隱式下沉處理。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es78-dont-rely-on-implicit-fallthrough-in-switch-statements




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

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

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


          瀏覽 52
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          <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>
                  国产永久在线 | 欧美91精品 | 国产原创AV成人网站 | 青草青青青在线免费观看视频 | A片在线免费观看 |