C++核心準(zhǔn)則?SF.10:避免依賴隱式包含的名稱

SF.10: Avoid dependencies on implicitly?#included names
SF.10:避免依賴隱式包含的名稱
Reason(原因)
Avoid surprises. Avoid having to change?#includes if an?#included header changes. Avoid accidentally becoming dependent on implementation details and logically separate entities included in a header.
避免意外。避免因?yàn)榘念^文件的變更引起包含指令的變化。避免偶然依賴實(shí)現(xiàn)細(xì)節(jié)并從邏輯上分離某個(gè)頭文件中包含的實(shí)體。
Example, bad(反面示例)
#include
using namespace std;
void use()
{
string s;
cin >> s; // fine
getline(cin, s); // error: getline() not defined
if (s == "surprise") { // error == not defined
// ...
}
}
The solution is to explicitly?#include
解決方案就是顯式包含
Example, good(范例)
#include
#include
using namespace std;
void use()
{
string s;
cin >> s; // fine
getline(cin, s); // fine
if (s == "surprise") { // fine
// ...
}
}Note(注意)
Some headers exist exactly to collect a set of consistent declarations from a variety of headers. For example:
有些頭文件只是為了匯集了一套選自大量頭文件的聲明而存在,例如:
// basic_std_lib.h:
#include
#include a user can now get that set of declarations with a single?#include"
現(xiàn)在用戶可以使用一條包含指令引入一套聲明:
#include "basic_std_lib.h"
This rule against implicit inclusion is not meant to prevent such deliberate aggregation.
本規(guī)則反對(duì)隱式包含,但無(wú)意阻止這種有意識(shí)的組合。
Enforcement(實(shí)施建議)
Enforcement would require some knowledge about what in a header is meant to be "exported" to users and what is there to enable implementation. No really good solution is possible until we have modules.
實(shí)施建議需要某些關(guān)于頭文件意圖向用戶暴露什么和有什么可以讓實(shí)現(xiàn)有效的知識(shí)。直到模塊功能可用之前都不會(huì)有真正完美的解決方案。
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#sf10-avoid-dependencies-on-implicitly-included-names
新書(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ō)明,讓讀者明白在編寫代碼時(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>
