C++核心準則T.10:為所有的模板引數定義概念

一個樣本是什麼意思

C++核心準則T.10:為所有的模板引數定義概念

石竹

T。10: Specify concepts for all template arguments

T。10:為所有的模板引數定義概念

Reason(原因)

Correctness and readability。 The assumed meaning (syntax and semantics) of a template argument is fundamental to the interface of a template。 A concept dramatically improves documentation and error handling for the template。 Specifying concepts for template arguments is a powerful design tool。

正確性和可讀性。一個模板引數的假定含義(語法和語義)是模板介面的基礎。概念大幅度改善了模板的文件化和錯誤處理。為模板引數定義概念是一個強有力的設計工具。

Example(例項)

template// requires Input_iterator// && Equality_comparable, Val>Iter find(Iter b, Iter e, Val v){ // 。。。}

or equivalently and more succinctly:

或者使用下面功能等價但更簡潔的方式:

template// requires Equality_comparable, Val>Iter find(Iter b, Iter e, Val v){ // 。。。}

Note(注意)

“Concepts” are defined in an ISO Technical Specification: concepts。 A draft of a set of standard-library concepts can be found in another ISO TS: ranges 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:

“概念”被ISO技術規格:concepts定義。一套標準庫concepts的初步版本可以在另一個ISO技術規格:ranges中找到。GCC6。1以後都支援concepts。因此我們在例項程式碼中註釋掉使用concepts的部分;也就是說我們只是將它們用作標準的註釋。如果你使用GCC6。1之後的版本,可以開啟註釋。

template requires Input_iterator && Equality_comparable, Val>Iter find(Iter b, Iter e, Val v){ // 。。。}

Note(注意)

Plain typename (or auto) is the least constraining concept。 It should be used only rarely when nothing more than “it‘s a type” can be assumed。 This is typically only needed when (as part of template metaprogramming code) we manipulate pure expression trees, postponing type checking。

直接的型別名(或auto)是最小約束的概念。它應該被極少使用,僅限於表現“它是一個型別”。這通常只在我們操作純表示式樹,延遲型別檢查時有(作為模板超程式設計的一部分)存在的必要。

References: TC++PL4, Palo Alto TR, Sutton

參見:TC++PL4, Palo Alto TR, Sutton

Enforcement(實施建議)

Flag template type arguments without concepts

標記沒有使用概念的模板型別引數。

原文連結

https://github。com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines。md#t10-specify-concepts-for-all-template-arguments

新書介紹

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

C++核心準則T.10:為所有的模板引數定義概念

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

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

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

關注微信公眾號【面向物件思考】輕鬆學習每一天!

面向物件開發,面向物件思考!