您好,登錄后才能下訂單哦!
本篇內容主要講解“C++的模板概念怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C++的模板概念怎么使用”吧!
T.41:在模板概念中只對本質屬性定義需求
Keep interfaces simple and stable.
維持接口的簡單和穩定。
Example (using TS concepts)(示例(使用TS概念))
Consider, a sort instrumented with (oversimplified) simple debug support:
考慮一種包含(過于簡單了)簡單的調試功能的排序處理:
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:
應該這樣寫:
template<Sortable S>
requires Streamable<S>
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中沒有任何需要iostream支持的東西。同樣,排序的基本想法中也沒有任何關于調試的需求。
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.
如果我們要求所有用到的操作都被羅列在需求中,接口的可用性就會降低:每次我們改變調試功能,用法數據收集,測試支持,錯誤報告,等等,模板的定義都需要修改,并且每個使用模板的代碼都必須重新編譯。這種方式很笨拙,在某些環境中也是無法做到的。
Conversely, if we use an operation in the implementation that is not guaranteed by concept checking, we may get a late compile-time error.
相反,如果我們使用某個沒有被概念檢查保證的實現中的操作,我們可能得到遲到的編譯時錯誤。
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.
通過不用概念檢查非本質模板參數的屬性,我們將檢查延遲到實例化時。我們認為這是一種值得的妥協。
Note that using non-local, non-dependent names (such as debug and cerr) also introduces context dependencies that may lead to "mysterious" errors.
注意,使用非局部,獨立名稱(例如debug和cerr)也會引入可能導致“神秘”錯誤的上下文依賴。
Note(注意)
It can be hard to decide which properties of a type are essential and which are not.
很難決定類型的那個屬性是本質的,那個屬性不是本質的。
到此,相信大家對“C++的模板概念怎么使用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。