2015-07-18 30 views
16

請考慮以下程序。根據C++標準是它良好的成立(該標準的相關部分引用所需的):嘗試通過使用聲明來定義名稱空間成員

namespace X { extern int i; } 

namespace N { using X::i; } 

int N::i = 1; 

int main() {} 

我得到不同的編譯器不同的結果。我試圖找出什麼編譯器我應該立案的bug報告:

  • 鏘:提供了以下編譯器錯誤:沒有名爲「我」在命名空間「N」

  • GCC成員和Visual C++編譯它沒有錯誤。

爲便於比較,下面給出編譯錯誤,與所有三個編譯器:

namespace X { void f(); } 

namespace N { using X::f; } 

void N::f() {}; 

int main() {} 
+1

有趣,VS2013編譯,但IntelliSence說''錯誤:命名空間「N」沒有真正的成員「我」# – AlexD

+0

我想答案在這裏:[鏈接](http://stackoverflow.com/questions/6175705/scope-of-using-declaration-within-a-namespace) – Jorj

+0

@Supremum你可以從https://isocpp.org/std/the-standard獲得自己的C++標準副本。 – Coder

回答

11

當前工作草案N4527,[8.3p1]:

[...] When the declarator-id is qualified, the declaration shall refer to a previously declared member of the class or namespace to which the qualifier refers (or, in the case of a namespace, of an element of the inline namespace set of that namespace (7.3.1)) or to a specialization thereof; the member shall not merely have been introduced by a using-declaration in the scope of the class or namespace nominated by the nested-name-specifier of the declarator-id. [...]

所以,絕對病態的; GCC和MSVC是錯誤的。

相關問題