c++ - 如何解释标准中的以下段落?

标签 c++ c++11 namespaces scope language-lawyer

§3.3.6/1 (C++11)

The declarative region of a namespace-definition is its namespace-body. The potential scope denoted by an original-namespace-name is the concatenation of the declarative regions established by each of the namespace-definitions in the same declarative region with that original-namespace-name....

声明区域的定义如下(§3.3.1/1):

Every name is introduced in some portion of program text called a declarative region, which is the largest part of the program in which that name is valid, that is, in which that name may be used as an unqualified name to refer to the same entity. ...

两者结合在一起似乎意味着命名空间的名称只能在命名空间主体内部使用(非限定)。但是,很明显,这是错误的。那么当命名空间的名称实际上可以在命名空间主体之外使用(非限定)时,命名空间定义的声明区域是主体意味着什么?

此外,我根本不明白这一点(从上面重新引用):

The potential scope denoted by an original-namespace-name is the concatenation of the declarative regions established by each of the namespace-definitions in the same declarative region with that original-namespace-name.

最佳答案

标准不是在谈论你的想法......

我认为您的困惑在于您错误地假设文本是关于命名空间名称本身的,而标准实际上是在谈论在相关命名空间内部引入的名称。 p>


那引用的“只是没有意义”呢?

The potential scope denoted by an original-namespace-name is the concatenation of the declarative regions established by each of the namespace-definitions in the same declarative region with that original-namespace-name.


上面的例子可能最容易描述:

.--- namespace definition
|        .--- original-namespace-name
v        v
namespace N { <-------------------------------.
  int x = 0;                              <---+--- declarative region (1)
} <-------------------------------------------'

.--- another namespace definition of `N`
|         .--- original-namespace-name
v         v
namespace N { <-------------------------------.
  int y = x;                              <---+--- declarative region (2)
} <-------------------------------------------'

注意:int x 的潜在范围是(1)(2),即。 引入了“声明区域的串联”

  • 在示例中我们有两个 namespace-definitions 用于 original-namespace-name N,我们还有两个 < em>声明性区域,但名为 N 的命名空间内的“潜在范围”既是(1),又是(2)

  • 只要命名空间定义本身在同一个声明区域,并且共享相同的original-namespace-name ,它们指的是同一个命名空间。

  • 引入另一个这样的命名空间定义只会为潜在范围添加更多空间(通过附加另一个声明区域)先前在其中声明的变量。


潜在范围声明区域;它们是什么?

  • 声明区域 是程序的一部分,名称可以在其中被引用而无需限定。

  • 潜在范围是名称​​可能有效的范围,它是名称 < strong>可以指代同一实体。


3.3.1p1 Declarative regions and scope [basic.scope.declarative]

Every name is introduced in some portion of program text called a declarative region, which is the largest part of the program in which that name is valid, that is, in which the name may be used as an unqualified name to refer to the same entity.

In general, each particular name is valid only within some possible discontiguous portion of program text called its scope. To determine the scope of a declaration, it is sometimes conventient to refer to the potential scope of a declaration.

The scope of a declaration is the same as its potential scope unless the potential scope contains another declaration of the same name. In that case the potential scope of the declaration in the inner (contained) declarative region is excluded from the scope of the declaration in the outer (containing) declarative region.

关于c++ - 如何解释标准中的以下段落?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23966921/

相关文章:

c++ - 引用静态数据成员

c++ - 魔术静力学保证右侧只执行一次吗?

c++ - 如何从文件中读取输入并将其配对到 C++ 中的映射中

c++ - 减少参数数量

c++ - 在不膨胀命名空间的情况下在 C++ 中声明枚举的好方法

c++ - 正确使用 std::launder

c++ - std::unique_lock<mutex> 和 conditional_variable cond 的关系

c++ - 获取 std::string 容器的 C 字符串迭代器

c# - .NET 4.5 命名空间 'Standard'

c# - 为多个服务设置 ASMX 命名空间的最佳方法