问题描述
有时候我会找到类似以下的代码(实际上有些类向导会创建这样的代码):
Sometimes I find code like the following (actually some class-wizards create such code):
// C.h
namespace NS {
class C {
void f();
};
}
并在实施文件中:
// C.cpp
#include "C.h"
using namespace NS;
void C::f() {
//...
}
我尝试过的所有编译器都接受这种代码(gcc,clang,msvc,compileonline.com). using namespace NS;
使我感到不舒服.从我的角度来看,C::f()
驻留在全局名称空间中,该环境对存在于名称空间NS中的对象具有不合格的访问权限.但是编译器认为void C::f()
存在于namespace NS
中.当我尝试过的所有编译器都同意这种观点时,他们可能是正确的,但是这种观点在标准中有何根据?
All the compilers I tried accept that kind of code (gcc, clang, msvc, compileonline.com). What makes me feel uncomfortable is the using namespace NS;
. From my point of view C::f()
lives in the global namespace in an environment that has unqualified access to objects living in namespace NS. But in the compiler's opinion void C::f()
lives in namespace NS
. As all compilers I tried share that point of view they are probably right, but where in the standard is this opinion backed?
推荐答案
是的,语法确实合法,但是否,您的函数确实存在于命名空间NS中 >.您所看到的代码实际上等效于
Yes, the syntax is indeed legal, but no, your function actually does live in the namespace NS. The code you are seeing is actually equivalent to
namespace NS { void C::f() { /* ... } }
或
void NS::C::f() { /* ... */ }
这可能更像您的习惯.
由于using指令,您不仅可以在调用代码中而且在其定义中省略NS部分.该标准有一个与您的代码匹配的示例(在粗体强调的部分之后):
Because of the using-directive you can omit the NS part not only in calling code, but also in its definition. The Standard has an example that matches your code (after the bold emphasized part):
3.4.3.2命名空间成员[namespace.qual]
namespace A {
namespace B {
void f1(int);
}
using namespace B;
}
void A::f1(int){ } // ill-formed, f1 is not a member of A
namespace A {
namespace B {
void f1(int);
}
}
namespace C {
namespace D {
void f1(int);
}
}
using namespace A;
using namespace C::D;
void B::f1(int){ } // OK, defines A::B::f1(int)
因此,您可以省略嵌套名称说明符的初始部分,但没有任何中间部分.
So you may omit the initial part of the nested-name-specifier, but not any intermediate part.
这篇关于是否可以在声明它们的名称空间之外定义类成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!