Enumerations

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

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
 
E

Eric Sosman

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

Vladimir Oka

Christian said:
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.
 
D

Default User

Eric said:
Christian Christmann wrote On 05/17/06 11:08,:
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
 
C

Christian Christmann

Eric said:
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
 
V

Vladimir Oka

Christian said:
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,108) 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.
 
E

Eric Sosman

[Attribution lost; the next paragraph is by Default User]
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top