C++核心準(zhǔn)則T.60:最小化模板對(duì)上下文的依賴

T.60: Minimize a template's context dependencies
T.60:最小化模板對(duì)上下文的依賴
Reason(原因)
Eases understanding. Minimizes errors from unexpected dependencies. Eases tool creation.
讓理解更容易。盡量減少由于意外的依賴引發(fā)的錯(cuò)誤。方便工具生成。
Example(示例)
template
void sort(C& c)
{
std::sort(begin(c), end(c)); // necessary and useful dependency
}
template
Iter algo(Iter first, Iter last)
{
for (; first != last; ++first) {
auto x = sqrt(*first); // potentially surprising dependency: which sqrt()?
helper(first, x); // potentially surprising dependency:
// helper is chosen based on first and x
TT var = 7; // potentially surprising dependency: which TT?
}
}Note(注意)
Templates typically appear in header files so their context dependencies are more vulnerable to?#include?order dependencies than functions in?.cpp?files.
模板通常出現(xiàn)在頭文件中,相比在CPP文件中的情況,它們的上下文依賴會(huì)更容易受到#include次序的影響。
Note(注意)
Having a template operate only on its arguments would be one way of reducing the number of dependencies to a minimum, but that would generally be unmanageable. For example, algorithms usually use other algorithms and invoke operations that do not exclusively operate on arguments. And don't get us started on macros!
讓模板操作僅僅操作參數(shù)是一種將依賴個(gè)數(shù)減少到最小的一種方式。但是那樣通常會(huì)無(wú)法實(shí)現(xiàn)。例如,算法通常會(huì)使用其他算法并調(diào)用無(wú)法對(duì)參數(shù)進(jìn)行排他操作的操作。
See also:?T.69
參見(jiàn):T.69
Enforcement(實(shí)施建議)
??? Tricky
不好辦
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t60-minimize-a-templates-context-dependencies
新書(shū)介紹
《實(shí)戰(zhàn)Python設(shè)計(jì)模式》是作者最近出版的新書(shū),拜托多多關(guān)注!

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