c - 为什么 C 有单独的标签和标识符的 namespace ?

标签 c namespaces

来自 cppreference :

1) Label name space: all identifiers declared as labels.

2) Tag names: all identifiers declared as names of structs, unions and enumerated types.

3) Member names: all identifiers declared as members of any one struct or union. Every struct and union introduces its own name space of this kind.

4) All other identifiers, called ordinary identifiers to distinguish from (1-3) (function names, object names, typedef names, enumeration constants).

这允许这样的代码(除其他外):

struct Point { int x, y; };
struct Point Point;

这段代码对我来说似乎有点不清楚,因为 Point 可以引用结构的类型和实例。为标签和其他标识符设置单独的 namespace 背后的动机是什么?

最佳答案

实际提出的问题是

What was the motivation behind having separate name spaces for tags and other identifiers?

这只能通过引用标准委员会的基本原理文件来回答,该文件实际上确实解决了这个问题,无论多么简短:

Pre-C89 implementations varied considerably in the number of separate name spaces maintained. The position adopted in the Standard is to permit as many separate name spaces as can be distinguished by context, except that all tags (struct, union, and enum) comprise a single name space.

(C99 rationale 文件,* 6.2.3 节)

因此,像

这样的代码是明确有意的
struct point { int point; } point = { .point = 0 };
goto point;
point:
return point.point;

是允许的。我对基本原理的解释是,其意图是不受限制的,尽管目前还不清楚为什么不同种类的标签没有被赋予单独的命名空间。这不可能是偶然的,所以委员会中的一个或多个代表一定反对单独的标签命名空间,并且他们设法占了上风。这种反对很可能是出于商业原因而不是技术原因。


*据我所知,C2011 标准没有基本原理文档。至少,现在还没有。

关于c - 为什么 C 有单独的标签和标识符的 namespace ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57539467/

相关文章:

xml - 在 XSLT 中不引用默认命名空间

c - 使用指向数值数据的 void* 指针的算术运算

c - 无法打开 FIFO 进行写入

c - 在 c 中使用 getopt 的选项

在 XmlParse/序列化后丢失的属性值中使用的 groovy xml 命名空间定义

namespaces - Doctrine 2 命名空间问题

c - 链接到不同版本的 SSL 共享库

比较两个分数(< 和 friend )

php - 如何解析php上的命名空间?

c# - 不能包含扩展的 WPF 工具包