Scope of incomplete types

William M. Miller wmm at charis.UUCP
Thu Sep 6 05:12:21 AEST 1990


As far as I can tell, X3.159-1989 does not define what happens in the
following situation:
 
                struct X* xp1;
                {
                   struct X* xp2;
                   }
 
The question is whether xp1 and xp2 have the same type.  On the affirmative
side, there doesn't appear to be anything in the Standard to indicate any
difference in the way the scope rules apply to incomplete types.  The
following wording from 3.5.2.3 would seem to allow (but perhaps not to
require?) this interpretation:
 
        A declaration of the form
 
                struct-or-union identifier ;
 
        specifies a structure or union type and declares a tag, both visible
        only within the scope in which the declaration occurs.  It specifies
        a new type distinct from any type with the same tag in an enclosing
        scope (if any).
 
If "any type with the same tag" includes incomplete types, the implication
is that an incomplete type with the same tag in an enclosing type *would* be
visible, apart from such a declaration.
 
The commentary on one of the examples in 3.5.2.3 appears to be conclusive:
 
        ...the declarations
 
                struct s1 { struct s2 *s2p; /*...*/ }; /* D1 */
                struct s2 { struct s1 *s1p; /*...*/ }; /* D2 */
 
        specify a pair of structures that contain pointers to each other.
        Note, however, that if s2 were already declared as a tag in an
        enclosing scope, the declaration D1 would refer to *it*, not to the
        tag s2 declared in D2.
 
Since the declaration of xp1 declares X as a tag in the enclosing block,
this wording would indicate that the declaration of xp2 refers to the same
tag and that, therefore, xp1 and xp2 have the same type.
 
There are basically two factors supporting the opposite conclusion.  The
first is the wording used in 3.5.2.3 of the Rationale: "if struct y is
already *defined* [emphasis mine] in a containing block..."  This wording
would tend to indicate that a full definition, not simply an incomplete
type, is required in the containing block for the type to be visible.
 
To illustrate the second consideration, consider the following slightly
expanded version of the example with which I opened this posting:
 
                struct X* xp1;
                {
                   struct X* xp2;
                   }
                struct X { /*...*/ };   /* D1 */
 
3.5.2.3 says about incomplete struct-or-union types,
 
        If the type is to be completed, another declaration of the tag in
        the same scope (but not in an enclosed block, which declares a new
        type known only within that block) shall define the content.
 
If xp1 and xp2 have the same type, and the declaration D1 completes the type
introduced by the declaration of xp1, then it also completes the type in the
declaration of xp2, in violation of the above-quoted rule requiring such
completion to be in the same scope.
 
Does anyone out there in netland have anything to say about this question?
 
------------------------------------------------------------------------------
William M. Miller, Glockenspiel, Inc.; P. O. Box 366, Sudbury, MA 01776-0003
wmmiller at cup.portal.com         BIX: wmiller            CI$: 72105,1744
-- 
Non-disclaimer:  My boss and I always see eye-to-eye (every time I look in
the mirror).

wmm at sdti.sdti.com



More information about the Comp.std.c mailing list