<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.11:使用auto避免多余的類型名重復(fù)

          0b117a8dd9065cfef525c7de8b408d38.webp

          ES.11: Use?auto?to avoid redundant repetition of type names

          ES.11:使用auto避免多余的類型名重復(fù)


          Reason(原因)

          • Simple repetition is tedious and error-prone.

            簡(jiǎn)單的重復(fù)多余且易錯(cuò)。

          • When you use?auto, the name of the declared entity is in a fixed position in the declaration, increasing readability.

            當(dāng)你使用auto的時(shí)候,被定義實(shí)體的名稱會(huì)出現(xiàn)在固定的位置,這樣可以增加可讀性。??????????

          • In a template function declaration the return type can be a member type.

            在模板函數(shù)定義中,返回值可以是成員類型。


          Example(示例)

          Consider:

          考慮以下代碼:

          auto p = v.begin();   // vector::iterator
          auto h = t.future();
          auto q = make_unique(s);
          auto f = [](int x){ return x + 10; };

          In each case, we save writing a longish, hard-to-remember type that the compiler already knows but a programmer could get wrong.

          無論哪種情況,我們都不必編寫又長(zhǎng)、類型又難記的類型信息。其實(shí)這些信息編譯器已經(jīng)知道,但程序員還是會(huì)弄錯(cuò)。


          Example(示例)

          template
          auto Container::first() -> Iterator; // Container::Iterator

          Exception(例外)

          Avoid?auto?for initializer lists and in cases where you know exactly which type you want and where an initializer might require conversion.

          對(duì)于你確切地知道所需類型但初始化器可能需要轉(zhuǎn)換的情況,應(yīng)避免為初始化列表使用auto。


          Example(示例)

          auto lst = { 1, 2, 3 };   // lst is an initializer list
          auto x{1}; // x is an int (in C++17; initializer_list in C++11)

          Note(注意)

          When concepts become available, we can (and should) be more specific about the type we are deducing:

          在concepts可以之后,我們可以(也應(yīng)該)更加明確我們推斷的類型。

          // ...
          ForwardIterator p = algo(x, y, z);
          Example (C++17)

          示例(C++17)

          auto [ quotient, remainder ] = div(123456, 73);   // break out the members of the div_t result

          Enforcement(實(shí)施建議)

          Flag redundant repetition of type names in a declaration.

          標(biāo)記在聲明時(shí)發(fā)生的多余的類型名稱重復(fù)。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es11-use-auto-to-avoid-redundant-repetition-of-type-names




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

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

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


          瀏覽 56
          點(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在线中文字幕 | 香蕉视频911 | 精品一区一区三区四区 | 影音先锋福利 | sm调教视频网站 |