<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++核心準則T.20:避免定義沒有明確語義的“概念”

          共 2815字,需瀏覽 6分鐘

           ·

          2020-08-28 10:36

          蜀葵

          T.20: Avoid "concepts" without meaningful semantics

          T.20:避免定義沒有明確語義的“概念”


          Reason(原因)

          Concepts are meant to express semantic notions, such as "a number", "a range" of elements, and "totally ordered." Simple constraints, such as "has a?+?operator" and "has a?>?operator" cannot be meaningfully specified in isolation and should be used only as building blocks for meaningful concepts, rather than in user code.

          概念意在表現(xiàn)某種有語義的觀念,例如“數(shù)字”,元素的“范圍”,和“完全有序”等。簡單的約束,例如有+運算符,有>運算符不能算作被獨立,明確地定義,只應用于某個明確概念的組成部分,而不是在代碼中直接使用。


          Example, bad (using TS concepts)

          反面示例(使用TS概念)

          template
          concept Addable = has_plus; // bad; insufficient

          template auto algo(const N& a, const N& b) // use two numbers
          {
          // ...
          return a + b;
          }

          int x = 7;
          int y = 9;
          auto z = algo(x, y); // z = 16

          string xx = "7";
          string yy = "9";
          auto zz = algo(xx, yy); // zz = "79"

          Maybe the concatenation was expected. More likely, it was an accident. Defining minus equivalently would give dramatically different sets of accepted types. This?Addable?violates the mathematical rule that addition is supposed to be commutative:?a+b == b+a.

          也許期待的操作就是連結。更有可能的是,這只是一個意外。對等地定義減操作將會提供一套明顯不同的可接受類型。這個可加性違反了加法運算滿足交換律(a+b==b+a)這個規(guī)則。


          Note(注意)

          The ability to specify a meaningful semantics is a defining characteristic of a true concept, as opposed to a syntactic constraint.

          定義明確語義的能力是真正的概念所具備的明確特征,而不是句法約束。


          Example (using TS concepts)

          示例(使用TS概念)

          template
          // The operators +, -, *, and / for a number are assumed to follow the usual mathematical rules
          concept Number = has_plus
          && has_minus
          && has_multiply
          && has_divide;

          template auto algo(const N& a, const N& b)
          {
          // ...
          return a + b;
          }

          int x = 7;
          int y = 9;
          auto z = algo(x, y); // z = 16

          string xx = "7";
          string yy = "9";
          auto zz = algo(xx, yy); // error: string is not a Number
          Note(注意)

          Concepts with multiple operations have far lower chance of accidentally matching a type than a single-operation concept.

          相比只有一個操作的概念,包含多個操作的概念偶然和某個類型匹配的可能性會大為降低。


          Enforcement(實施建議)

          • Flag single-operation?concepts?when used outside the definition of other?concepts.

          • 標記處于其他概念定義范圍之外的單操作概念。

          • Flag uses of?enable_if?that appears to simulate single-operation?concepts.

          • 標記將enable_if用于模擬單操作概念的情況。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t20-avoid-concepts-without-meaningful-semantics


          新書介紹

          《實戰(zhàn)Python設計模式》是作者最近出版的新書,拜托多多關注!

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

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




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

          關注微信公眾號【面向對象思考】輕松學習每一天!

          面向對象開發(fā),面向對象思考!



          瀏覽 40
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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无码人妻精品1国产动漫 | 韩无码在线 | 免费的成人网站在线观看 |