未命名的命名空间访问规则 [英] Unnamed namespace access rules

查看:200
本文介绍了未命名的命名空间访问规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看C ++ 03标准中的 7.3.1.1 部分,希望能找到对未命名命名空间中定义的项的访问规则的一些描述。

I was looking over section 7.3.1.1 in the C++03 standard expecting to find some description of the access rules for items defined in an unnamed namespace.

对于未命名的命名空间,规则似乎有点不同,因为您不能完全限定对其中一个项目的访问。我知道至少在同一个翻译单元中,可以访问未命名命名空间中的项,就像它们不在命名空间中一样。例如:

The rules seem to be a little different for unnamed namespaces, since you cannot fully qualify access to items in one. I know that at least within the same translation unit, one can access items in an unnamed namespace as if they were not in a namespace. For example:

namespace {
  int foo;
}

void something()
{
  foo = 4;
}

如果命名空间有一个名称,你不能这样做。

If the namespace had a name, you could not do this. So, where are the rules defined in the standard for these exceptional rules that apply to unnamed namespaces?

推荐答案

匿名命名空间基本上是一个匿名命名空间处理为:

An anonymous namespace is basically treated as:

namespace unique_per_TU
{
    // Stuff
}
using namespace unique_per_TU;

我会在一分钟内找到这里的参考。

I'll try to find the reference here in a minute.

编辑:

您已经在 7.3.1.1/1 / p>

It appears you already found it in 7.3.1.1/1


未命名的命名空间定义表现为被

An unnamed namespace definition behaves as if it were replaced by



namespace unique { /* empty body */ }
using namespace unique;
namespace unique { namespacebody }




in
a翻译单元由相同的标识符替换,并且此
标识符与整个程序中的所有其他标识符不同。

where all occurrences of unique in a translation unit are replaced by the same identifier and this identifier differs from all other identifiers in the entire program.

假使用已经将命名空间成员带入您发现的全局命名空间。

The "fake" using already brings the namespace members into the global namespace as you discovered.

这篇关于未命名的命名空间访问规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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