不要在頭文件中使用using namespace

不要在頭文件中使用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操作有效消除歧義和使用其他選項的能力。另外,文件以不同次序被包含時的含義可能會隨之不同,導致產生包含順序依賴性。

Example(原因)

<code> 
 

using

namespace

std

;

bool

copy

( )

;

int

main

()

{ copy( ); }/<code>

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命名而且他們不會和標準庫相沖突,使用using namespace std::literals是就可以認為是必要的。

Enforcement(實施建議)

Flag using namespace at global scope in a header file.

標記在頭文件的全局作用域中使用using namspace指令的情況。

原文鏈接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#sf7-dont-write-using-namespace-at-global-scope-in-a-header-file

新書介紹

《實戰Python設計模式》是作者最近出版的新書,拜託多多關注!

不要在頭文件中使用using namespace

本書利用Python 的標準GUI 工具包tkinter,通過可執行的示例對23 個設計模式逐個進行說明。這樣一方面可以使讀者瞭解真實的軟件開發工作中每個設計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進行說明,讓讀者明白在編寫代碼時如何判斷使用設計模式的利弊,併合理運用設計模式。

對設計模式感興趣而且希望隨學隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學習Python GUI 編程的讀者可以將本書中的示例作為設計和開發的參考;使用Python 語言進行圖像分析、數據處理工作的讀者可以直接以本書中的示例為基礎,迅速構建自己的系統架構。



覺得本文有幫助?請分享給更多人。

關注微信公眾號【面向對象思考】輕鬆學習每一天!

面向對象開發,面向對象思考!


分享到:


相關文章: