C11中类型名称范围的概念

前端之家收集整理的这篇文章主要介绍了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)是“标识符范围”.这个段落的措词对我来说不清楚:是一个“类型名称范围”,一种范围,类似于块范围,文件范围等?或者它是类型名称本身的范围(在这种情况下,还有一个问题是如何没有名称的对象有范围,什么是这种范围的可能用途).

2)这个定义在哪里呢?更具体地说,如果更改为在对应的类型名称完成后立即启动类型名称的范围,那会影响什么?可以遵循抽象声明符(= type name)中省略的标识符的唯一令牌是一些括号和参数名称或数组维度列表(内含表达式),它们都不能引用所讨论的类型名称,因为存在没有标识符可供参考.为什么不等到宣布者完成?

解决方法

这个措辞是 Defect Report #341: [*] in abstract declarators主题,它解释了C99标准类型名称没有范围但在某些情况下需要适用范围的问题,因此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.2段第4段中的措辞,该修改将:

[…]declarations with function prototype scope[…]

至:

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

原文链接:https://www.f2er.com/c/115877.html

猜你在找的C&C++相关文章