Why “using namespace X;” is not allowed inside class/struct level?

后端 未结 5 670
走了就别回头了
走了就别回头了 2020-11-27 15:35
class C {
  using namespace std;  // error
};
namespace N {
  using namespace std; // ok
}
int main () {
  using namespace std; // ok
}

Edi

5条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 16:09

    Because the C++ standard explicitly forbids it. From C++03 §7.3.4 [namespace.udir]:

    using-directive:
        using namespace ::opt nested-name-specifieropt namespace-name ;
    

    A using-directive shall not appear in class scope, but may appear in namespace scope or in block scope. [Note: when looking up a namespace-name in a using-directive, only namespace names are considered, see 3.4.6. ]

    Why does the C++ standard forbid it? I don't know, ask a member of the ISO committee that approved the language standard.

提交回复
热议问题