C++核心準(zhǔn)則SF.4:在其他聲明之前include .h文件

SF.4: Include?.h?files before other declarations in a file
SF.4:在其他聲明之前include .h文件
Reason(原因)
Minimize context dependencies and increase readability.
最小化上下文依賴性并提高可讀性。
Example(示例)
#include
#include
#include
// ... my code here ...Example, bad(反面示例)
#include
// ... my code here ...
#include
#include Note(注意)
This applies to both?.h?and?.cpp?files.
本規(guī)則.h文件和.cpp文件都適用。
Note(注意)
There is an argument for insulating code from declarations and macros in header files by?#including?headers?after?the code we want to protect (as in the example labeled "bad"). However
關(guān)于在頭文件中隔離代碼和聲明/宏的方式存在其他觀點(diǎn),這種觀點(diǎn)建議在需要保護(hù)的代碼之后include頭文件(就像我們注明反面示例的代碼)。然而:
that only works for one file (at one level): Use that technique in a header included with other headers and the vulnerability reappears.
這種方法只適用于一個(gè)文件(一層):一旦在一個(gè)被其他頭文件引用的頭文件中使用這個(gè)技術(shù),脆弱性會(huì)再次出現(xiàn)。
a namespace (an "implementation namespace") can protect against many context dependencies.
命名空間(“實(shí)現(xiàn)命名空間”)可以防止很多上下文依賴性。
full protection and flexibility require modules.
完全防止并保持靈活性需要模塊功能。
See also(參見):
Working Draft, Extensions to C++ for Modules:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4592.pdf
Modules, Componentization, and Transition:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0141r0.pdf
Enforcement(實(shí)施建議)
Easy.
容易。
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#sf4-include-h-files-before-other-declarations-in-a-file
新書介紹
《實(shí)戰(zhàn)Python設(shè)計(jì)模式》是作者最近出版的新書,拜托多多關(guān)注!

本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對23 個(gè)設(shè)計(jì)模式逐個(gè)進(jìn)行說明。這樣一方面可以使讀者了解真實(shí)的軟件開發(fā)工作中每個(gè)設(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)注微信公眾號(hào)【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
