为什么C对于标记和标识符有单独的名称空间?

时间:2021-11-18 12:32:43

From cppreference:

从cppreference:

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

1)标签名称空间:所有标识为标签的标识符。

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

2)标记名称:声明为结构体、联合和枚举类型名称的所有标识符。

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.

3)成员名称:作为任何一个结构体或联合的成员声明的所有标识符。每一个结构体和联合体都引入了自己的这种名称空间。

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

4)所有其他标识符,称为普通标识符以区别(1-3)(函数名、对象名、类型定义名、枚举常量)。

This allows for code like this (among other things):

这允许像这样的代码(包括其他东西):

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

This code seems somewhat unclear to me as Point can refer to both a type and an instance of a struct. What was the motivation behind having separate name spaces for tags and other identifiers?

这段代码在我看来有点不清楚,因为Point既可以指类型,也可以指结构体的实例。为标记和其他标识符设置单独的名称空间背后的动机是什么?

1 个解决方案

#1


1  

The actual question posed is

实际的问题是。

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

为标记和其他标识符设置单独的名称空间背后的动机是什么?

This can be answered only by reference to the standard committee's rationale document, which in fact does address the matter, however briefly:

只有参照标准委员会的基本原则文件才能回答这一问题,事实上,该文件确实处理了这一问题,但简短地说:

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.

c89之前的实现在维护的独立名称空间的数量上有很大的差异。标准中采用的位置是允许根据上下文区分尽可能多的独立名称空间,但是所有标记(struct、union和enum)都包含单个名称空间。

(C99 rationale document,* section 6.2.3)

(C99基本原理文件,*第6.2.3条)

Thus, it is explicitly intentional that code such as

因此,代码如

struct point { int point; } point = { .point = 0 };
goto point;
point:
return point.point;

is permitted. My interpretation of the rationale is that the intention was to be unrestrictive, though it remains unclear why the different kinds of tags were not given separate namespaces. This could not have been accidental, so one or more parties represented on the committee must have opposed separate tag namespaces, and they managed to prevail. Such opposition could very well have been for business instead of technical reasons.

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


*As far as I am aware, there is no rationale document for the C2011 standard. At least, not yet.

*据我所知,C2011标准没有基本原理。至少,现在还不是时候。

#1


1  

The actual question posed is

实际的问题是。

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

为标记和其他标识符设置单独的名称空间背后的动机是什么?

This can be answered only by reference to the standard committee's rationale document, which in fact does address the matter, however briefly:

只有参照标准委员会的基本原则文件才能回答这一问题,事实上,该文件确实处理了这一问题,但简短地说:

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.

c89之前的实现在维护的独立名称空间的数量上有很大的差异。标准中采用的位置是允许根据上下文区分尽可能多的独立名称空间,但是所有标记(struct、union和enum)都包含单个名称空间。

(C99 rationale document,* section 6.2.3)

(C99基本原理文件,*第6.2.3条)

Thus, it is explicitly intentional that code such as

因此,代码如

struct point { int point; } point = { .point = 0 };
goto point;
point:
return point.point;

is permitted. My interpretation of the rationale is that the intention was to be unrestrictive, though it remains unclear why the different kinds of tags were not given separate namespaces. This could not have been accidental, so one or more parties represented on the committee must have opposed separate tag namespaces, and they managed to prevail. Such opposition could very well have been for business instead of technical reasons.

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


*As far as I am aware, there is no rationale document for the C2011 standard. At least, not yet.

*据我所知,C2011标准没有基本原理。至少,现在还不是时候。