Velocity Reviews > Enumerations

# Enumerations

Christian Christmann
Guest
Posts: n/a

 05-17-2006
Hi,

is it valid to assign an enumeration the enumeration
value of another enumeration?

Example:

enum type1 { A = -9, B, C, D, E };
enum enum_type1 enum1 = A;
enum type2 { F = 10, G = 20, H = 30, I = 40, J = 50 };
int main( void )
{
enum type2 enum2 = A; //OK???

return 0;
}

Thank you.
Chris

jarod.tang@gmail.com
Guest
Posts: n/a

 05-17-2006
yep, you can do this, in fact, you can just view the A as a integer (
-9), you can get the enum's discritption at
http://msdn2.microsoft.com/en-US/library/whbyts4t.aspx or so, have fun

Eric Sosman
Guest
Posts: n/a

 05-17-2006

Christian Christmann wrote On 05/17/06 11:08,:
> Hi,
>
> is it valid to assign an enumeration the enumeration
> value of another enumeration?
>
> Example:
>
> enum type1 { A = -9, B, C, D, E };
> enum enum_type1 enum1 = A;

The `enum_' part doesn't belong.

> enum type2 { F = 10, G = 20, H = 30, I = 40, J = 50 };
> int main( void )
> {
> enum type2 enum2 = A; //OK???
>
> return 0;
> }

Yes. enum types are not "rigorous" in this sense.
The enum type itself is just some kind of an integer
(the compiler gets to choose), and the enumerated
values are just integer constants. There's no further
enforcement of type.

That said, some compilers may issue warnings for
some kinds of abuse of enum types. If they do, it's
just a courtesy, though, in the same way that some
compilers will issue a warning for `if (a = b)' --
the construct is valid, but the context is suspect.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

Guest
Posts: n/a

 05-17-2006

Christian Christmann wrote:
> Hi,
>
> is it valid to assign an enumeration the enumeration
> value of another enumeration?

Compiler certainly won't stop you, so I guess I'd say it's not invalid.
IMHO, C is broken in this respect.

However, I'd be worried about your design if you're planning to use
this "feature".

> Example:
>
> enum type1 { A = -9, B, C, D, E };
> enum enum_type1 enum1 = A;

ITYM:

enum type1 enum1 = A;

Speaks volumes for pasting rather than typing the code in a post.

> enum type2 { F = 10, G = 20, H = 30, I = 40, J = 50 };
> int main( void )
> {
> enum type2 enum2 = A; //OK???

Using C++ style comments (although valid in C99) is a bad idea in
posts.

> return 0;
> }

Guest
Posts: n/a

 05-17-2006

(E-Mail Removed) wrote:
> yep, you can do this,

> in fact, you can just view the A as a integer (
> -9), you can get the enum's discritption at
> http://msdn2.microsoft.com/en-US/library/whbyts4t.aspx or so, have fun

Default User
Guest
Posts: n/a

 05-17-2006
Eric Sosman wrote:

>
>
> Christian Christmann wrote On 05/17/06 11:08,:
> > Hi,
> >
> > is it valid to assign an enumeration the enumeration
> > value of another enumeration?

> Yes. enum types are not "rigorous" in this sense.
> The enum type itself is just some kind of an integer
> (the compiler gets to choose), and the enumerated
> values are just integer constants. There's no further
> enforcement of type.

I suppose that it could be unsafe in some circumstances, if one enum
had a smaller range than the other. You could get overflow.

Brian

Christian Christmann
Guest
Posts: n/a

 05-18-2006
> Eric Sosman wrote:

>> The enum type itself is just some kind of an integer
>> (the compiler gets to choose), and the enumerated
>> values are just integer constants. There's no further
>> enforcement of type.

>
> I suppose that it could be unsafe in some circumstances, if one enum
> had a smaller range than the other. You could get overflow.

How can this happen? Does not every enum has the same range namely
signed integer?

Paul

Guest
Posts: n/a

 05-18-2006

Christian Christmann wrote:
> > Eric Sosman wrote:

>
> >> The enum type itself is just some kind of an integer
> >> (the compiler gets to choose), and the enumerated
> >> values are just integer constants. There's no further
> >> enforcement of type.

> >
> > I suppose that it could be unsafe in some circumstances, if one enum
> > had a smaller range than the other. You could get overflow.

>
> How can this happen? Does not every enum has the same range namely
> signed integer?

Not necessarily:

6.7.2.2p4
Each enumerated type shall be compatible with
* char, a signed integer type, or an unsigned integer type.
The choice of type is implementation-defined,10 but
shall be capable of representing the values of all the
members of the enumeration.

Enumeraiton constants, though:

6.4.4.3p2
An identifier declared as an enumeration constant has type int.

Eric Sosman
Guest
Posts: n/a

 05-18-2006
Christian Christmann wrote:
>>Eric Sosman wrote:

>
>
>>>The enum type itself is just some kind of an integer
>>>(the compiler gets to choose), and the enumerated
>>>values are just integer constants. There's no further
>>>enforcement of type.

>>

[Attribution lost; the next paragraph is by Default User]

>>I suppose that it could be unsafe in some circumstances, if one enum
>>had a smaller range than the other. You could get overflow.

>
>
> How can this happen? Does not every enum has the same range namely
> signed integer?

No. The compiler chooses the type underlying each enum
type, and it may choose differently for different enums. It
must choose a type that is some kind of integer, and it must
choose a type that can represent all the enumerated values
for the enum, but there are no other restrictions.

For example, given

enum small { SMALL_A = -1, SMALL_B = 1 };
enum large { LARGE_A = 0, LARGE_B = 1 };

the compiler might choose `signed char' and `unsigned char'
as the two underlying types. (The type for `enum_small' must
be signed because SMALL_A is negative, but `enum_large' has
no negative enumerated values so an unsigned type is possible.)
Now if the programmer does

enum large x = -1; /* stores UCHAR_MAX */
enum small y = x; /* too large for `signed char' */

.... he runs the risk that Default User mentions. However, there
is still no "enforcement," in the sense that the compiler is not
required to issue a diagnostic for the potential bad behavior.

--
Eric Sosman
(E-Mail Removed)lid