gpt4 book ai didi

c++ - 未命名的命名空间访问规则

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:19:46 25 4
gpt4 key购买 nike

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

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

namespace {
int foo;
}

void something()
{
foo = 4;
}

如果命名空间有名称,则无法执行此操作。那么,对于这些适用于未命名命名空间的异常(exception)规则,标准中定义的规则在哪里?

最佳答案

匿名命名空间基本上被视为:

namespace unique_per_TU
{
// Stuff
}
using namespace unique_per_TU;

我会尽快在此处找到引用。

编辑:

看来您已经在 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 }

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.

如您所见,“假”使用已经将命名空间成员带入了全局命名空间。

关于c++ - 未命名的命名空间访问规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9622874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com