C++核心準(zhǔn)則?T.84:使用非模板核心實(shí)現(xiàn)提供穩(wěn)定的ABI接口

T.84: Use a non-template core implementation to provide an ABI-stable interface
T.84:使用非模板核心實(shí)現(xiàn)提供穩(wěn)定的ABI接口
Reason(原因)
Improve stability of code. Avoid code bloat.
提高代碼的穩(wěn)定性。避免代碼膨脹。
Example(示例)
It could be a base class:
它可以作為基類存在:
struct Link_base { // stable
Link_base* suc;
Link_base* pre;
};
template // templated wrapper to add type safety
struct Link : Link_base {
T val;
};
struct List_base {
Link_base* first; // first element (if any)
int sz; // number of elements
void add_front(Link_base* p);
// ...
};
template
class List : List_base {
public:
void put_front(const T& e) { add_front(new Link{e}); } // implicit cast to Link_base
T& front() { static_cast*>(first).val; } // explicit cast back to Link
// ...
};
List li;
List ls;
Now there is only one copy of the operations linking and unlinking elements of a?List. The?Link?and?List?classes do nothing but type manipulation.
(雖然例示了兩個List類,)對于List的關(guān)聯(lián)和非關(guān)聯(lián)元素來講,只有一套操作(函數(shù))的拷貝。Link和List除了類型操作之外不做任何事。
Instead of using a separate "base" type, another common technique is to specialize for?void?or?void*?and have the general template for?T?be just the safely-encapsulated casts to and from the core?void?implementation.
除了使用獨(dú)立的“基礎(chǔ)”類型,另外一個通用技術(shù)是定義基于void和void*類型的核心實(shí)現(xiàn)并準(zhǔn)備一個目的僅限于安全地封裝從或到void核心實(shí)現(xiàn)進(jìn)行轉(zhuǎn)換的通用模板類。
Alternative: Use a?Pimpl?implementation.
其他選項(xiàng):使用指向?qū)崿F(xiàn)的指針技術(shù)來實(shí)現(xiàn)。
Enforcement(實(shí)施建議)
???
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t84-use-a-non-template-core-implementation-to-provide-an-abi-stable-interface
新書介紹
《實(shí)戰(zhàn)Python設(shè)計(jì)模式》是作者最近出版的新書,拜托多多關(guān)注!

本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設(shè)計(jì)模式逐個進(jìn)行說明。這樣一方面可以使讀者了解真實(shí)的軟件開發(fā)工作中每個設(shè)計(jì)模式的運(yùn)用場景和想要解決的問題;另一方面通過對這些問題的解決過程進(jìn)行說明,讓讀者明白在編寫代碼時(shí)如何判斷使用設(shè)計(jì)模式的利弊,并合理運(yùn)用設(shè)計(jì)模式。
對設(shè)計(jì)模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運(yùn)用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計(jì)和開發(fā)的參考;使用Python 語言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺得本文有幫助?請分享給更多人。
關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
