定义命名空间成员与无限定的自由函数
我有时在嵌套命名空间中声明类,当涉及到定义它们的成员函数时,我不喜欢用这些嵌套命名空间名称来限定每个类,特别是如果它们很长的话。
在定义成员函数之前添加“using namespace”(或者,为了更精确的定位,“using ::SomeClass”)似乎不需要限定每个定义,但我在规范中找不到保证这一点的任何地方,我担心这可能是一种仅适用于 GCC 的行为。我注意到,似乎没有类似的机制可以在定义自由函数时跳过添加限定符的需要(?)。
作为我的意思的一个例子:
标题:
// example.h
namespace SomeNamespace
{
class SomeClass
{
public:
void someMemberFunction();
};
void someFreeFunction();
};
实现:
// example.cpp
#include "example.h"
using namespace SomeNamespace;
void SomeClass::someMemberFunction()
{
// OK: seems to define SomeNamespace::SomeClass::someMemberFunction(),
// even though we didn't qualify it with SomeNamespace::
}
void someFreeFunction()
{
// Not what we wanted; declares and defines ::someFreeFunction(), not
// SomeNamespace::someFreeFunction() (quite understandably)
}
int main()
{
SomeClass a;
a.someMemberFunction(); // Ok; it is defined above.
SomeNamespace::someFreeFunction(); // Undefined!
return 0;
}
所以我的问题:上面定义 SomeClass::someMemberFunction() 的方法是否合法,规范中的哪里提到了这一点?如果合法的话,是否值得推荐?它确实减少了混乱! :)
非常感谢:)
I sometimes declare classes in nested namespaces and when it comes to defining their member functions, I prefer not to have to qualify each one with these nested namespace names, especially if they are long-ish.
Adding "using namespace " (or, for more precise targetting, "using ::SomeClass") before I define the member functions seems to obviate the need to qualify each definition, but I can't find anywhere in the spec that guarantees this, and I'm worried that it might be a behaviour that only works with GCC. I note that there doesn't appear to be a similar mechanism for skipping the need to add the qualifiers when defining free functions(?).
As an example of what I mean:
Header:
// example.h
namespace SomeNamespace
{
class SomeClass
{
public:
void someMemberFunction();
};
void someFreeFunction();
};
Implementation:
// example.cpp
#include "example.h"
using namespace SomeNamespace;
void SomeClass::someMemberFunction()
{
// OK: seems to define SomeNamespace::SomeClass::someMemberFunction(),
// even though we didn't qualify it with SomeNamespace::
}
void someFreeFunction()
{
// Not what we wanted; declares and defines ::someFreeFunction(), not
// SomeNamespace::someFreeFunction() (quite understandably)
}
int main()
{
SomeClass a;
a.someMemberFunction(); // Ok; it is defined above.
SomeNamespace::someFreeFunction(); // Undefined!
return 0;
}
So my question: is the above way of definining SomeClass::someMemberFunction() legal, and where in the spec is this mentioned? If legal, is it advisable? It certainly cuts down on clutter! :)
Many thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许我弄错了,但如果你有:
你也可以简单地写:
Perhaps I'm getting this wrong but if you have:
You can also simply write:
当您定义成员函数时,编译器意识到它是一个必须属于先前声明的类的成员函数,因此它会向上查找该类,如标准第 9.3.5 节中所指定的:
基本上,你所做的一切都很好。但是,在使用嵌套命名空间或具有长名称的命名空间(或两者)时,还有另一种(首选)方法可以减少混乱 - 定义别名:
When you define a member-function, the compiler realizes that it is a member-function that must belong to a previously declared class, so it looks that class up, as specified in Section 9.3.5 of the standard:
Basically, what you are doing is fine. However, there is another (preferable) way to cut down on the clutter when using nested namespaces, or namespaces with long names (or both) - define an alias: