c - C11 中类型名称作用域的概念

标签 c scope c99 language-lawyer c11

ISO C11标准(我只能接触到一个草案版本,n1570.pdf)在6.2.1.8(C99标准中没有该段)中指出:

As a special case, a type name (which is not a declaration of an identifier) is considered to have a scope that begins just after the place within the type name where the omitted identifier would appear were it not omitted.

我正在寻找以下任何解释:

1) 部分 (6.2.1) 的名称是“标识符的范围”。我不清楚这一段的措辞:“类型名称范围”是一种范围,类似于例如 block 范围,文件范围等?或者它是类型名称本身的范围(在这种情况下,另一个问题是没有名称的对象如何具有范围以及这种范围的可能用途)。

2) 这个定义在哪里重要?更具体地说,如果将其更改为类型名称的范围在相应类型名称完成后立即开始,那会产生什么影响?抽象声明符(=类型名称)中省略的标识符后面可以跟随的唯一标记是几个括号和参数名称列表或数组维度(内部有表达式),它们都不能引用所讨论的类型名称,因为有没有可供引用的标识符。为什么不等到声明符完成?

最佳答案

这个措辞是Defect Report #341: [*] in abstract declarators的主题,它讨论了 C99 标准 type-name 没有作用域但在某些情况下需要作用域的问题,因此 C99 需要修复。缺陷报告说(强调我的):

6.7.5.2#4 says that * as an array size "can only be used in declarations with function prototype scope", and paragraph 5 says "If the size is an expression that is not an integer constant expression: if it occurs in a declaration at function prototype scope, it is treated as if it were replaced by *".

But is a type name in a function prototype a declaration, and does it have function prototype scope? Scopes are only defined in 6.2.1 for identifiers, and such type names do not declare identifiers. The presence of [*] in the syntax for abstract declarators suggests that

void f(int (*)[*]);

was intended to be valid and void f(int (*)[a]); was intended to be equivalent to it, but there are no declarations at function prototype scope involved. [...]

当前的措辞是这个问题的解决方案,评论包括以下内容:

It appears the issue hinges entirely on the point that a type-name is not a declaration and does not declare an identifier, and because of that it has no scope. Instead of adding complex wording to avoid using the term "scope" as suggested in the DR, it seems clearer to modify the definition of Scope such that it applies to type-name, which is described in 6.7.6 as "syntactically a declaration for a function or an object of that type that omits the identifier".

这也会影响 6.7.5.24 中的措辞,该段将短语从:

[...]declarations with function prototype scope[...]

到:

[...]declarations or type-names with function prototype scope[...]

关于c - C11 中类型名称作用域的概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22313488/

相关文章:

将指针的值更改为c函数中的指针

c - linux mkdir函数无法授予完全权限

c++ - C/C++ 工作流引擎

c - 为什么这个程序在 C11 中编译得很好,但在 C99 中却编译不好?

c - 线性搜索保存到文件的结构中的元素

JavaScript 无法在 ExtJS 5 中获取对表单的引用

javascript - 如何从传递给 CasperJS 的评估函数中调用另一个函数

PHP:将函数范围限制在当前文件内

C - complex.h 在不同操作系统上的意外行为

c - 实际参数的指针不匹配?