This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: G++ enums 'underlying type'
- From: Paul Schlie <schlie at comcast dot net>
- To: Gabriel Dos Reis <gdr at integrable-solutions dot net>
- Cc: <gcc at gcc dot gnu dot org>
- Date: Wed, 29 Dec 2004 16:44:51 -0500
- Subject: Re: G++ enums 'underlying type'
> From: Gabriel Dos Reis <gdr@integrable-solutions.net>
> |> Paul Schlie <schlie@comcast.net> writes:
> | > From: Gabriel Dos Reis <gdr@integrable-solutions.net>
> | > | Paul Schlie <schlie@comcast.net> writes:
> | > | > From: Paolo Carlini <pcarlini at suse dot de>
> | > | > assuming something specific about g++ 'underlying type' (7.2/5) will
> alow
> | > me
> | > | > to remarkably simplifiy the implementation of tr1/type_traits/is_enum:
> is
> | > it
> | > | > signed or not? Basing on some simple experiments on x86, seems always
> wide
> | > | > either like an int or a long: is this correct in general?
> | > |
> | > | To my understanding, C enums need only be represented as the smallest
> rank
> | > | signed or unsigned integer type capable of representing it's value range
> | > | (although GCC tends to first unnecessarily promote enums to int, which I
> | > | believe is officially the largest enum equivalent type, and then back
> them
> | > | down opportunistically; keeping their converted form as small as
> possible is
> | > | significant to small machine targets, so they should not be
> unnecessarily
> | > | represented as being any larger than necessary, just as bool/char/short
> | > | types should not be needlessly promoted).
> | >
> | > In C, enumerator are ints -- even though the associated enumeration
> | > type is considered a distinct types. Only C++ allows for the smallest
> | > integer type as the underlying type; and C++ enumerators are not ints.
> |
> | Converted to integer type of compatible rank, not "int"
>
> You're confusing an "enumerated type" ("enumeration" in C++ speak)
> with "enumerator", i.e. the named constants.
>
> | 6.3 Conversions
> | ...
> |
> | -- The rank of any enumerated type shall equal the rank of
> | the compatible integer type.
>
> Irrelevant.
>
> If you're quoting the C standard, see 6.7.2.2/3
>
> Semantics
>
> [#3] The identifiers in an enumerator list are declared as
> constants that have type int and may appear wherever such
> are permitted.107) An enumerator with = defines its
> enumeration constant as the value of the constant
> expression. If the first enumerator has no =, the value of
> its enumeration constant is 0. Each subsequent enumerator
> with no = defines its enumeration constant as the value of
> the constant expression obtained by adding 1 to the value of
> the previous enumeration constant. (The use of enumerators
> with = may produce enumeration constants with values that
> duplicate other values in the same enumeration.) The
> enumerators of an enumeration are also known as its members.
>
> Enumerators are ints, in C.
>
> In C++, they are not.
>
> In both languages, the enumeration (i.e. the type declared with the
> keyword "enum") is a distinct type.
>
> -- Gaby
Agreed, to clarify: an enumeration (enumerated type, i.e. enum)
representation need be no larger than the smallest compatible integer
type, which I believe was the question. Enumerators, as you've noted
above are specified as int sized constants during an enumeration's
declaration, as it's unknown until the declaration is completed what
the range, therefore necessary size, of the enumeration and it's
correspondingly member representations need be.
We agree?
Thanks, -paul-