天天看點

C++核心準則T.26:相比簡單的文法,根據使用模式術語定義概念更好

T.26: Prefer to define concepts in terms of use-patterns rather than simple syntax

T.26:相比簡單的文法,根據使用模式術語定義概念更好

Reason(原因)

The definition is more readable and corresponds directly to what a user has to write. Conversions are taken into account. You don't have to remember the names of all the type traits.

這種定義方式更具可讀性,和使用者必須寫的内容直接對應。轉換也考慮在内。你不必記住所有類型的特征。

Example (using TS concepts)(示例(使用TS概念))

You might be tempted to define a concept Equality like this:

你可能試圖像這樣定義Equality概念:

template<typename T> concept Equality = has_equal<T> && has_not_equal<T>;      

Obviously, it would be better and easier just to use the standard EqualityComparable, but - just as an example - if you had to define such a concept, prefer:

顯然,隻是使用标準的EqualityComparable會更好,也更容易,但是隻是一個例子,如果你必須定義這樣的概念,這樣更好:

template<typename T> concept Equality = requires(T a, T b) {
    bool == { a == b }
    bool == { a != b }
    // axiom { !(a == b) == (a != b) }
    // axiom { a = b; => a == b }  // => means "implies"
}      

as opposed to defining two meaningless concepts has_equal and has_not_equal just as helpers in the definition of Equality. By "meaningless" we mean that we cannot specify the semantics of has_equal in isolation.

而不是隻為了輔助定義Equality而定義無意義的has_equal和has_not_equal概念。通過“無意義”這個詞,我們想表達的是我們無法獨立地定義has_equal的語義。

Enforcement(實施建議)

???

原文連結

​​https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t26-prefer-to-define-concepts-in-terms-of-use-patterns-rather-than-simple-syntax​​

新書介紹

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

C++核心準則T.26:相比簡單的文法,根據使用模式術語定義概念更好

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

對設計模式感興趣而且希望随學随用的讀者通過本書可以快速跨越從了解到運用的門檻;希望學習Python GUI 程式設計的讀者可以将本書中的示例作為設計和開發的參考;使用Python 語言進行圖像分析、資料處理工作的讀者可以直接以本書中的示例為基礎,迅速建構自己的系統架構。

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

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

面向對象開發,面向對象思考!

繼續閱讀