本篇內(nèi)容主要講解“C++的模板概念怎么使用”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“C++的模板概念怎么使用”吧!
專(zhuān)注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)本溪免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
T.41:在模板概念中只對(duì)本質(zhì)屬性定義需求
Keep interfaces simple and stable.
維持接口的簡(jiǎn)單和穩(wěn)定。
Example (using TS concepts)(示例(使用TS概念))
Consider, a sort instrumented with (oversimplified) simple debug support:
考慮一種包含(過(guò)于簡(jiǎn)單了)簡(jiǎn)單的調(diào)試功能的排序處理:
void sort(Sortable& s) // sort sequence s
{
if (debug) cerr << "enter sort( " << s << ")\n";
// ...
if (debug) cerr << "exit sort( " << s << ")\n";
}
Should this be rewritten to:
應(yīng)該這樣寫(xiě):
template
requires Streamable
void sort(S& s) // sort sequence s
{
if (debug) cerr << "enter sort( " << s << ")\n";
// ...
if (debug) cerr << "exit sort( " << s << ")\n";
}
After all, there is nothing in Sortable that requires iostream support. On the other hand, there is nothing in the fundamental idea of sorting that says anything about debugging.
畢竟Sortable中沒(méi)有任何需要iostream支持的東西。同樣,排序的基本想法中也沒(méi)有任何關(guān)于調(diào)試的需求。
Note(注意)
If we require every operation used to be listed among the requirements, the interface becomes unstable: Every time we change the debug facilities, the usage data gathering, testing support, error reporting, etc., the definition of the template would need change and every use of the template would have to be recompiled. This is cumbersome, and in some environments infeasible.
如果我們要求所有用到的操作都被羅列在需求中,接口的可用性就會(huì)降低:每次我們改變調(diào)試功能,用法數(shù)據(jù)收集,測(cè)試支持,錯(cuò)誤報(bào)告,等等,模板的定義都需要修改,并且每個(gè)使用模板的代碼都必須重新編譯。這種方式很笨拙,在某些環(huán)境中也是無(wú)法做到的。
Conversely, if we use an operation in the implementation that is not guaranteed by concept checking, we may get a late compile-time error.
相反,如果我們使用某個(gè)沒(méi)有被概念檢查保證的實(shí)現(xiàn)中的操作,我們可能得到遲到的編譯時(shí)錯(cuò)誤。
By not using concept checking for properties of a template argument that is not considered essential, we delay checking until instantiation time. We consider this a worthwhile tradeoff.
通過(guò)不用概念檢查非本質(zhì)模板參數(shù)的屬性,我們將檢查延遲到實(shí)例化時(shí)。我們認(rèn)為這是一種值得的妥協(xié)。
Note that using non-local, non-dependent names (such as debug and cerr) also introduces context dependencies that may lead to "mysterious" errors.
注意,使用非局部,獨(dú)立名稱(chēng)(例如debug和cerr)也會(huì)引入可能導(dǎo)致“神秘”錯(cuò)誤的上下文依賴(lài)。
Note(注意)
It can be hard to decide which properties of a type are essential and which are not.
很難決定類(lèi)型的那個(gè)屬性是本質(zhì)的,那個(gè)屬性不是本質(zhì)的。
到此,相信大家對(duì)“C++的模板概念怎么使用”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!