为什么C的标签和标识符具有单独的名称空间? [英] Why does C have separate name spaces for tags and identifiers?

查看:83
本文介绍了为什么C的标签和标识符具有单独的名称空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 cppreference :

1)标签名称空间:所有声明为标签的标识符.

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

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

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

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

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)所有其他标识符,称为与(1-3)区别的普通标识符(函数名称,对象名称,typedef名称,枚举常量).

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

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

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

struct Point { int x, y; };
struct Point 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?

推荐答案

提出的实际问题是

为标签和其他标识符使用单独的名称空间的背后动机是什么?

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:

C89之前的实现在所维护的单独名称空间的数量上有很大不同.该标准所采用的位置是允许根据上下文区分尽可能多的独立名称空间,除了所有标签(结构,联合和枚举)都包含一个名称空间.

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原理文档, * 第6.2.3节)

(C99 rationale document,* section 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.

* 据我所知,没有C2011标准的基本文件.至少还没有.

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

这篇关于为什么C的标签和标识符具有单独的名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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