C++核心準(zhǔn)則SF.7:不要在頭文件中的全局作用域中使用using namespace指令

SF.7: Don't write?using namespace?at global scope in a header file
SF.7:不要在頭文件中的全局作用域中使用using namespace指令
Reason(原因)
Doing so takes away an?#includer's ability to effectively disambiguate and to use alternatives. It also makes?#included headers order-dependent as they might have different meaning when included in different orders.
這么做去除了include操作有效消除歧義和使用其他選項的能力。另外,文件以不同次序被包含時的含義可能會隨之不同,導(dǎo)致產(chǎn)生包含順序依賴性。
Example(原因)
// bad.h
#include
using namespace std; // bad
// user.cpp
#include "bad.h"
bool copy(/*... some parameters ...*/); // some function that happens to be named copy
int main()
{
copy(/*...*/); // now overloads local ::copy and std::copy, could be ambiguous
}Note(注意)
An exception is?using namespace std::literals;. This is necessary to use string literals in header files and given?the rules?- users are required to name their own UDLs?operator""_x?- they will not collide with the standard library.
有一個例外是using namspace std::literals;。如果需要在頭文件中使用字符串字面值而且滿足這樣的條件:用戶被要求為他們自己的UDL運算符“”_x命名而且他們不會和標(biāo)準(zhǔn)庫相沖突,使用using namespace std::literals是就可以認(rèn)為是必要的。
Enforcement(實施建議)
Flag?using namespace?at global scope in a header file.
標(biāo)記在頭文件的全局作用域中使用using namspace指令的情況。
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#sf7-dont-write-using-namespace-at-global-scope-in-a-header-file
新書介紹
《實戰(zhàn)Python設(shè)計模式》是作者最近出版的新書,拜托多多關(guān)注!

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