Article View: comp.std.c
Article #2892Re: How soon can `sizeof (tagged_type)' be used?
From: Bruce Blodgett
Date: Thu, 16 May 1991 19:44
Date: Thu, 16 May 1991 19:44
53 lines
2552 bytes
2552 bytes
In article <1991May13.175830.7143@zoo.toronto.edu> henry@zoo.toronto.edu (Henry Spencer) writes:
>In article <5506@lupine.NCD.COM> rfg@lupine.ncd.com (Ron Guilmette) writes:
>>Are either or both of these examples legal?
>>
>>enum E { red, green = sizeof (enum E), blue };
>>struct S { int i; int j : sizeof (struct S); int k; };
>
>I think neither.
>
>In the first one, the argument of sizeof is meaningless, because E is not
>declared until the end of the brace-enclosed list. Enums, unlike structs
>and unions, cannot be incomplete types.
Section 3.1.2.1 states:
"enumeration tags have scope that begins just aftert the appearance of
the tag in a type specifier that declares the tag."
So enum tag E is in scope at the sizeof(enum E).
Section 3.3.3.4 lists the following Constraint:
"The sizeof operator shall not be applied to an expression that has ...
an incomplete type"
The question becomes, "does enum E have incomplete type inside its
enumerator-list?"
Section 3.1.2.5 defines incomplete types as:
"types that describe objects but lack information needed to determine
their sizes".
The question becomes, "does enum E lack information needed to
determine its size inside its enumerator-list?"
Section 3.5.2.2 states:
"Each enumerated type shall be compatible with an integer type; the
choice of type is implementation-defined."
The implementation could choose exactly one integral type with which
to make all enumerated types compatible. In this case, enum E has
enough information to determine its size inside its enumerator-list.
Alternately, the implementation could tailor each enumerated type to
be the smallest integral type sufficient to store any of its member's
values (to save on disk space). In this case, enum E does NOT have
enough information to determine its size inside its enumerator-list.
Therefore the definition
enum E { red, green = sizeof (enum E), blue };
relies upon implementation-defined behavior.
Bruce Blodgett
blodgett@apollo.hp.com
(508) 256-0176 x4037
Technical Details
Message-ID:
<519a28cf.20b6d@apollo.HP.COM>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!apollo!blodgettReferences:
<5506@lupine.NCD.COM> <1991May13.175830.7143@zoo.toronto.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!apollo!blodgett From: blodgett@apollo.HP.COM (Bruce Blodgett) Newsgroups: comp.std.c Subject: Re: How soon can `sizeof (tagged_type)' be used? Message-ID: <519a28cf.20b6d@apollo.HP.COM> Date: 16 May 91 19:44 GMT References: <5506@lupine.NCD.COM> <1991May13.175830.7143@zoo.toronto.edu> Sender: root@apollo.HP.COM Lines: 53 In-reply-to: henry@zoo.toronto.edu's message of 13 May 91 17:58 GMT