为什么每个人都使用未命名的命名空间声明(即std :: not :: std::)? [英] Why does everybody use unanchored namespace declarations (i.e. std:: not ::std::)?

查看:132
本文介绍了为什么每个人都使用未命名的命名空间声明(即std :: not :: std::)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,使用unanchored命名空间只是要求麻烦以后当有人放在一个新的命名空间,恰好与根级别命名空间相同的名称,神秘地改变了大量的程序的意义。所以,为什么人们总是说 std :: 而不是 :: std :: 。他们真的是说我想使用 std 很方便,而不是根本。

It seems to me that using unanchored namespaces is just asking for trouble later when someone puts in a new namespace that happens to have the same name as a root level namespace and mysteriously alters the meaning of a whole lot of programs. So, why do people always say std:: instead of ::std::. Do they really mean to be saying "I want to use whatever std is handy, not the root one."?

这里是我的意思的例子:

Here is an example of what I mean:

在fred / Foo.h:

In fred/Foo.h:

#include <string>

namespace fred {

class Foo {
 public:
   void aPublicMember(const std::string &s);
};

} // end namespace fred

在fred / Bar.h:

In fred/Bar.h:

namespace fred {
namespace std {  // A standard fred component

class string { // Something rather unlike the ::std::string
   // ...
};

} // namespace std

class Bar {
 public:
   void aPublicMember(std::string &s);
};

} // namespace fred

在oops.cpp:

#include <string>
#include "fred/Bar.h"
#include "fred/Foo.h"  // Oops, the meaning of Foo is now different.

这是人们想要的,还是我缺少的东西?

Is that what people want, or am I missing something?

也许你说你不应该命名命名空间 std 。这一切都很好,但是一些其他根级别的命名空间呢?如果任何根级别命名空间任何人曾经定义任何地方总是对子命名空间名称的限制?

And maybe you say that you should just never name a namespace std. And that's all well and good, but what about some other root level namespace then? Should any root level namespace anybody ever defines anywhere always be off-limits for a sub-namespace name?

为了澄清,我不会考虑任何答案告诉我 std 是特殊的,因为我只是使用它作为例子。我在说一个一般的问题,我使用 std 作为道具来说明它,虽然我承认这是一个令人吃惊的道具。

To clarify, I won't consider any answer that tells me std is special because I just used it as an example. I'm talking about a general issue, and I'm using std as a prop to illustrate it, though I do admit it's a rather startling prop.

推荐答案

未锚定的命名空间的实际原因是一级命名空间通常就足够了。当不是,通常将使用第二级别用于实现细节。最后,即使使用多个级别,它们仍然通常从根级隐式指定。即。即使在命名空间 ns1 中,通常还是引用 ns1 :: ns2 :: foo ,而不是 ns2 :: foo :: ns1 :: ns2 :: foo

The practical reason for unanchored namespaces is that one level of namespaces usually is enough. When it isn't, a second level is usually going to be used for implementation details. And finally, even when using multiple levels, they are still usually specified implicitly from root level. ie. even inside namespace ns1, you'd typically refer to ns1::ns2::foo instead of ns2::foo or ::ns1::ns2::foo.

因此,由于这三个原因,在正常情况下 :: ns1 形式是多余的。唯一的例子,我会考虑它将提交给Boost,因为作为一个Boost作者,我不知道我的软件将在哪里使用。

So, for these three reasons the ::ns1 form is redundant in normal cases. The only case where I'd consider it would be in submissions to Boost, because as a Boost author I won't know where my software will be used.

这篇关于为什么每个人都使用未命名的命名空间声明(即std :: not :: std::)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆