溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++怎么為模板參數(shù)定義概念

發(fā)布時間:2021-11-24 11:23:22 來源:億速云 閱讀:153 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要講解了“C++怎么為模板參數(shù)定義概念”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“C++怎么為模板參數(shù)定義概念”吧!

T.10:為所有的模板參數(shù)定義概念

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.

正確性和可讀性。一個模板參數(shù)的假定含義(語法和語義)是模板接口的基礎(chǔ)。概念大幅度改善了模板的文檔化和錯誤處理。為模板參數(shù)定義概念是一個強有力的設(shè)計工具。

Example(實例)

template<typename Iter, typename Val>
//    requires Input_iterator<Iter>
//             && Equality_comparable<Value_type<Iter>, Val>
Iter find(Iter b, Iter e, Val v)
{
   // ...
}

or equivalently and more succinctly:

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

template<Input_iterator Iter, typename Val>
//    requires Equality_comparable<Value_type<Iter>, 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技術(shù)規(guī)格:concepts定義。一套標(biāo)準(zhǔn)庫concepts的初步版本可以在另一個ISO技術(shù)規(guī)格:ranges中找到。GCC6.1以后都支持concepts。因此我們在實例代碼中注釋掉使用concepts的部分;也就是說我們只是將它們用作標(biāo)準(zhǔn)的注釋。如果你使用GCC6.1之后的版本,可以打開注釋。

template<typename Iter, typename Val>

    requires Input_iterator<Iter>
          && Equality_comparable<Value_type<Iter>, 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)是最小約束的概念。它應(yīng)該被極少使用,僅限于表現(xiàn)“它是一個類型”。這通常只在我們操作純表達式樹,延遲類型檢查時有(作為模板元編程的一部分)存在的必要。

感謝各位的閱讀,以上就是“C++怎么為模板參數(shù)定義概念”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對C++怎么為模板參數(shù)定義概念這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI