<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.74:盡量在循環(huán)變量初始化表達(dá)式中定義循環(huán)變量?

          9706fffe6386206f384d5651cf44b347.webp

          ES.74: Prefer to declare a loop variable in the initializer part of a?for-statement

          ES.74:盡量在循環(huán)變量初始化表達(dá)式中定義循環(huán)變量


          Reason(原因)

          Limit the loop variable visibility to the scope of the loop. Avoid using the loop variable for other purposes after the loop.

          將循環(huán)變量的作用域限制在循環(huán)之內(nèi)。避免在循環(huán)之后將循環(huán)變量用于其他目的。


          Example(示例)

          for (int i = 0; i < 100; ++i) {   // GOOD: i var is visible only inside the loop
          // ...
          }

          Example, don't(反面示例)
          int j;                            // BAD: j is visible outside the loop
          for (j = 0; j < 100; ++j) {
          // ...
          }
          // j is still visible here and isn't needed

          See also:?Don't use a variable for two unrelated purposes

          參見(jiàn):不用將變量用于兩個(gè)不同的目的。


          Example(示例)

          for (string s; cin >> s; ) {
          cout << s << '\n';
          }

          Enforcement(實(shí)施建議)

          Warn when a variable modified inside the?for-statement is declared outside the loop and not being used outside the loop.

          如果發(fā)現(xiàn)一個(gè)變量在for語(yǔ)句外部定義,在循環(huán)內(nèi)部被修改,同時(shí)沒(méi)有在循環(huán)外沒(méi)有被使用的情況,發(fā)出警告。

          Discussion: Scoping the loop variable to the loop body also helps code optimizers greatly. Recognizing that the induction variable is only accessible in the loop body unblocks optimizations such as hoisting, strength reduction, loop-invariant code motion, etc.

          討論:將循環(huán)變量的作用域限制在循環(huán)體之內(nèi)非常有利于代碼優(yōu)化。需要認(rèn)識(shí)到:只在循環(huán)體內(nèi)部才是可訪問(wèn)的歸納變量是很多優(yōu)化的必要條件:變量提升,強(qiáng)度削減,循環(huán)不變代碼外提等。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es74-prefer-to-declare-a-loop-variable-in-the-initializer-part-of-a-for-statement




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

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

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


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

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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片 | 一区在线观看视频 |