Unnamed Namespaces Replace File Statics

C++ Primer 4/e在Namespaces這個地方有一段說明:『Prior to the introduction of namespaces in standard C++, programs had to declare names as static to make them local to a file. The use of file statics is inherited from C. In C, a global entity declared static is invisible outside the file in which it is declared.


The use of file static declarations is deprecated by the C++ standard. A deprecated feature is one that may not be supported in future releases. File statics should be avoided and unnamed namespaces used instead.』

中文版的這樣寫:『標準C++引入namespace之前,程式必須將某個名稱宣告為static才能使它區域化(local to)於檔案內。這種性質繼承自C語言。在C語言中,宣告為static的global物體,在其所處檔案之外是不可見的。


C++不贊成使用file static變數宣告。這意味C++將來有可能不再支援此一特性。』


Print Friendly, PDF & Email


發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料