template标签_C++核心准则T.65:使用标签分发提供函数的不同实现
T.65: Use tag dispatch to provide alternative implementations of a function
T.65:使用標簽分發提供函數的不同實現
Reason(原因)
- A template defines a general interface.模板定義普遍接口。
- Tag dispatch allows us to select implementations based on specific properties of an argument type.標簽分發允許我們根據參數類型的特定屬性選擇實現方式。
- Performance.性能
Example(示例)
This is a simplified version of std::copy (ignoring the possibility of non-contiguous sequences)
這是std::copy的簡化版本(忽略非連續序列)
struct pod_tag {};struct non_pod_tag {};template struct copy_trait { using tag = non_pod_tag; }; // T is not "plain old data"template<> struct copy_trait { using tag = pod_tag; }; // int is "plain old data"templateOut copy_helper(Iter first, Iter last, Iter out, pod_tag){ // use memmove}templateOut copy_helper(Iter first, Iter last, Iter out, non_pod_tag){ // use loop calling copy constructors}templateOut copy(Iter first, Iter last, Iter out){ return copy_helper(first, last, out, typename copy_trait::tag{})}void use(vector& vi, vector& vi2, vector& vs, vector& vs2){ copy(vi.begin(), vi.end(), vi2.begin()); // uses memmove copy(vs.begin(), vs.end(), vs2.begin()); // uses a loop calling copy constructors}This is a general and powerful technique for compile-time algorithm selection.
這是一個可以在編譯時選擇算法的普遍和強大的技術。
Note(注意)
When concepts become widely available such alternatives can be distinguished directly:
當概念可以被普遍使用時,這樣的選項可以直接區分:
template requires Pod>Out copy_helper(In, first, In last, Out out){ // use memmove}templateOut copy_helper(In, first, In last, Out out){ // use loop calling copy constructors}Enforcement(實施建議)
???
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t65-use-tag-dispatch-to-provide-alternative-implementations-of-a-function
新書介紹
《實戰Python設計模式》是作者最近出版的新書,拜托多多關注!
本書利用Python 的標準GUI 工具包tkinter,通過可執行的示例對23 個設計模式逐個進行說明。這樣一方面可以使讀者了解真實的軟件開發工作中每個設計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進行說明,讓讀者明白在編寫代碼時如何判斷使用設計模式的利弊,并合理運用設計模式。
對設計模式感興趣而且希望隨學隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學習Python GUI 編程的讀者可以將本書中的示例作為設計和開發的參考;使用Python 語言進行圖像分析、數據處理工作的讀者可以直接以本書中的示例為基礎,迅速構建自己的系統架構。
覺得本文有幫助?請分享給更多人。
關注微信公眾號【面向對象思考】輕松學習每一天!
面向對象開發,面向對象思考!
總結
以上是生活随笔為你收集整理的template标签_C++核心准则T.65:使用标签分发提供函数的不同实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 酱油和gbt酱油哪个好_酱油越贵越好?认
- 下一篇: mysql timdir_MYSQL学习