C++核心準(zhǔn)則T.61:不要過度參數(shù)化成員(SCARY)

T.61: Do not over-parameterize members (SCARY)
T.61:不要過度參數(shù)化成員(SCARY)
Reason(原因)
A member that does not depend on a template parameter cannot be used except for a specific template argument. This limits use and typically increases code size.
不依賴于模板參數(shù)的成員無法使用,特定的模板參數(shù)除外。這會(huì)限制使用并通常會(huì)增加代碼大小。
Example, bad(反面示例)
template
// requires Regular && Allocator
class List {
public:
struct Link { // does not depend on A
T elem;
T* pre;
T* suc;
};
using iterator = Link*;
iterator first() const { return head; }
// ...
private:
Link* head;
};
List lst1;
List lst2;
This looks innocent enough, but now?Link?formally depends on the allocator (even though it doesn't use the allocator). This forces redundant instantiations that can be surprisingly costly in some real-world scenarios. Typically, the solution is to make what would have been a nested class non-local, with its own minimal set of template parameters.
代碼看起來足夠正確,但是現(xiàn)在Link形式上依賴于分配器(即使它沒有使用分配器)。這會(huì)引發(fā)多余的例示,而這種例示在某些現(xiàn)實(shí)流程中可能會(huì)帶來令人驚訝的高成本。通常的解決方案是讓本來的嵌套類別弄成非局部的,同時(shí)它的成員只擁有最少的模板參數(shù)。
template
struct Link {
T elem;
T* pre;
T* suc;
};
template
// requires Regular && Allocator
class List2 {
public:
using iterator = Link*;
iterator first() const { return head; }
// ...
private:
Link* head;
};
List lst1;
List lst2;
Some people found the idea that the?Link?no longer was hidden inside the list scary, so we named the technique?SCARY. From that academic paper: "The acronym SCARY describes assignments and initializations that are Seemingly erroneous (appearing Constrained by conflicting generic parameters), but Actually work with the Right implementation (unconstrained bY the conflict due to minimized dependencies)."
有些人會(huì)發(fā)現(xiàn)Link不再被list隱藏,因此我們稱這種技術(shù)為SCARY。根據(jù)大學(xué)論文:“SCARY這個(gè)縮寫描述了一些看起來錯(cuò)誤(看起來被沖突的參數(shù)約束),但實(shí)際上可以和正確的實(shí)現(xiàn)一起工作(由于最小化的依賴關(guān)系而不會(huì)被沖突所限制)的賦值和初始化。”
論文鏈接:
http://www.open-std.org/jtc1/sc22/WG21/docs/papers/2009/n2911.pdf
Enforcement(實(shí)施建議)
Flag member types that do not depend on every template argument
標(biāo)記不依賴于任何模板參數(shù)的成員
Flag member functions that do not depend on every template argument
標(biāo)記不依賴于任何模板參數(shù)的成員的成員函數(shù)。
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t61-do-not-over-parameterize-members-scary
新書介紹
《實(shí)戰(zhàn)Python設(shè)計(jì)模式》是作者最近出版的新書,拜托多多關(guān)注!

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