<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)則T.1:使用模板提高代碼的抽象水平

          月季

          T.1: Use templates to raise the level of abstraction of code

          T.1:使用模板提高代碼的抽象水平


          Reason(原因)

          Generality. Reuse. Efficiency. Encourages consistent definition of user types.

          普遍性。重用。效率。鼓勵(lì)用戶類型的一致性。


          Example, bad(反面示例)

          Conceptually, the following requirements are wrong because what we want of?T?is more than just the very low-level concepts of "can be incremented" or "can be added":

          概念上,我們希望T不僅限于可以進(jìn)行增量操作或者可以作為被加數(shù)這樣非常低水平的概念,因此下面的需求是錯(cuò)誤的。

          template
          // requires Incrementable
          T sum1(vector& v, T s)
          {
          for (auto x : v) s += x;
          return s;
          }

          template
          // requires Simple_number
          T sum2(vector& v, T s)
          {
          for (auto x : v) s = s + x;
          return s;
          }

          Assuming that?Incrementable?does not? support+ and Simple_number does not support +=, we have overconstrained implementers of sum1 and sum2. And, in this case, missed an opportunity for a generalization.

          假設(shè)Incrementable不支持+而且Simple_number不支持+=,我們過分約束了sum1和sum2的實(shí)現(xiàn)者。而且,在這種情況下,失去了泛化的機(jī)會(huì)。

          Example(示例)

          template
          // requires Arithmetic
          T sum(vector& v, T s)
          {
          for (auto x : v) s += x;
          return s;
          }

          Assuming that?Arithmetic?requires both?+?and?+=, we have constrained the user of?sum?to provide a complete arithmetic type. That is not a minimal requirement, but it gives the implementer of algorithms much needed freedom and ensures that any?Arithmetic?type can be used for a wide variety of algorithms.

          假設(shè)算術(shù)運(yùn)算既需要+也需要+=,我們已經(jīng)要求sum的用戶提供完全的算術(shù)類型。這不是最小化的需求,但是它為算法的實(shí)現(xiàn)者提供了所需的更多自由,而且保證算術(shù)類型可以用于多種多樣的算法。

          For additional generality and reusability, we could also use a more general?Container?or?Range?concept instead of committing to only one container,?vector.

          為了額外的泛用性和重用性,我們也可以使用更通用的容器或范圍概念代替特定的容器vector。


          Note(注意)

          If we define a template to require exactly the operations required for a single implementation of a single algorithm (e.g., requiring just?+=?rather than also?=?and?+) and only those, we have overconstrained maintainers. We aim to minimize requirements on template arguments, but the absolutely minimal requirements of an implementation is rarely a meaningful concept.

          如果我們定義了一個(gè)要求用于特定算法的特定實(shí)現(xiàn)的操作的模板(例如只要求+=而不同時(shí)要求=和+)而且只要求這些,我們就過分約束維護(hù)者了。我們的目的在于最小化模板參數(shù)的需求,但是某一實(shí)現(xiàn)的絕對(duì)最小需求幾乎不會(huì)成為有意義的概念。


          Note(注意)

          Templates can be used to express essentially everything (they are Turing?complete), but the aim of generic programming (as expressed using templates) is to efficiently generalize operations/algorithms over a set of types with similar semantic properties.

          模板可以用于從本質(zhì)上表達(dá)任何東西(它們具備圖靈完備性),但是泛型編程的目的(像使用模板表達(dá)的那樣)是高效概括可以適用于具有相似語義屬性一套類型的操作/算法。


          Note(注意)

          The?requires?in the comments are uses of?concepts. "Concepts" are defined in an ISO Technical Specification:?concepts. Concepts are supported in GCC 6.1 and later. Consequently, we comment out uses of concepts in examples; that is, we use them as formalized comments only. If you use GCC 6.1 or later, you can uncomment them.

          注釋中的需求是concept的用法?!癈oncepts”被定義在ISO技術(shù)規(guī)格中。GCC6.1之后的版本都支持Concepts。因此,在例子中我們注釋掉相關(guān)代碼;也就是說,我們只將它們用作標(biāo)準(zhǔn)注釋。如果你使用GCC6.1之后的版本,你可以去掉注釋符號(hào)。

          譯者注:concepts文檔鏈接:

          http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4553.pdf


          Enforcement(實(shí)施建議)

          • Flag algorithms with "overly simple" requirements, such as direct use of specific operators without a concept.

          • 標(biāo)記需求過于簡(jiǎn)單的算法,例如不用concept而直接使用特定操作符。

          • Do not flag the definition of the "overly simple" concepts themselves; they may simply be building blocks for more useful concepts.

          • 不要標(biāo)記定義過于簡(jiǎn)單的concept本身;它們沒準(zhǔn)只是作為某個(gè)更有用的concept的一部分存在的。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t1-use-templates-to-raise-the-level-of-abstraction-of-code


          新書介紹

          《實(shí)戰(zhàn)Python設(shè)計(jì)模式》一書是作者最近出版的新書,拜托多多關(guān)注!

          本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對(duì)23 個(gè)設(shè)計(jì)模式逐個(gè)進(jìn)行說明。這樣一方面可以使讀者了解真實(shí)的軟件開發(fā)工作中每個(gè)設(shè)計(jì)模式的運(yùn)用場(chǎng)景和想要解決的問題;另一方面通過對(duì)這些問題的解決過程進(jìn)行說明,讓讀者明白在編寫代碼時(shí)如何判斷使用設(shè)計(jì)模式的利弊,并合理運(yùn)用設(shè)計(jì)模式。

          對(duì)設(shè)計(jì)模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運(yùn)用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計(jì)和開發(fā)的參考;使用Python 語言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。




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

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

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



          瀏覽 33
          點(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>
                  国产精品无码专区 | 熟妇人妻中文AV无码 | 99久久99久久精品免费看蜜桃 | 亚洲日韩黄色电影 | 一级黄色天堂网片 |