10

ISO C11 Standard (I only have access to a draft version, n1570.pdf) states in 6.2.1.8 (there is no such paragraph in C99 standard):

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.

I am looking for any explanation of the following:

1) The name of the section (6.2.1) is "Scopes of identifiers". The wording of this paragraph is unclear to me: is a 'type name scope' a kind of scope, similar to, e.g. block scope, file scope, etc? Or is it the scope of the type name itself (in which case a further question is how can an object without a name have a scope and what is a possible use for such scope).

2) Where does this definition matter? More specifically, if it were changed to say that the scope of a type name starts immediately after the corresponding type name is complete, what would that affect? The only tokens that can follow the omitted identifier in the abstract declarator(=type name) are a few parentheses and a list of parameter names or array dimensions (with expressions inside) neither of which can refer to the type name in question since there is no identifier to reference. Why not wait till the declarator is complete?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
alexsh
  • 1,121
  • 8
  • 12
  • +1 That you took the time to dissect the cite in the standard is reason enough for an up vote on this. – WhozCraig Mar 10 '14 at 23:53
  • FYI: The wording in ISO/IEC 9899:2011 §6.2.1 ¶8 is identical to what you quote. – Jonathan Leffler Mar 11 '14 at 00:01
  • I'm not sure, but is the wording related to `struct xyz { ... };` which is a type name (`struct xyz`) that is not the declaration of an identifier? It would be considered to have a scope from the semicolon to the end of the enclosing scope (file if it appears outside any function; block if it appears inside a function). – Jonathan Leffler Mar 11 '14 at 00:04
  • Well, the scope of `xyz` in this case starts right before `{` (or right after `xyz` in the second case) whereas the wording in question refers to the scope that would start after `}'. The aswer below referring to DR341 is what I was looking for. Thanks for all the help! – alexsh Mar 11 '14 at 01:52

1 Answers1

4

This wording is the subject of Defect Report #341: [*] in abstract declarators, which discuses the problem that in the C99 standard type-name has no scope but scope is required in certain instances and so C99 required a fix. The defect report says (emphasis mine):

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. [...]

the current wording is the resolution of this issue the comments include the folowing:

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".

this is also effects the wording in section 6.7.5.2 paragraph 4 which changes the phrase from:

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

to:

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

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • For your last comment, did you mean 6.7.6.2(4)? Thanks a lot for the explanation! I have been searching for an appropriate DR but was unsuccessful. Thanks again. – alexsh Mar 11 '14 at 01:47
  • @alexsh it is `6.7.5.2` in the C99 draft and `6.7.6.2` in the C11 draft. Also this previous [question](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents) is useful for links to the draft standards. – Shafik Yaghmour Mar 11 '14 at 01:50
  • @alexsh no problem, this is fun for me. I enjoy questions that make me think. Although sometimes these language lawyer questions don't have satisfying answers. – Shafik Yaghmour Mar 11 '14 at 01:59