enums 
Author Message
 enums

I have always been of the opinion that enums in C are deficient because the
compatible integer type is implementation defined.

This has two distinct serious disadvantages:
1. they cannot be used for the basic elements of large data structures where
space is at a premium,
2. they cannot be portably used where the enumeration constants are outside
the range of an int.

The solution to situation 1 is to use char types, however it is very
probable that a de{*filter*} will not display the values in a symbolic fashion.
It might have the ability to be told to display them using the constants of
some enum, but even then ideally you want it displayed that way by default.

In situation 2, one can use the preprocessor if you don't want the type, but
merely the ability of enums to define compile time constants, but many of us
with good reason dislike the preprocessor. Or you could possibly use const,
but that may involve wasted space depending on how smart your linker is.

A possible extension to C has occurred to me that would solve these
problems. Give the programmer the ability to specify the underlying type in
the enum definition. For example:

typedef unsigned long enum
{
    big_value= 0xFFFFFFFF;

Quote:
} big_enum_t;

Now one could be certain that big_enum_t would be compatible with unsigned
long and that big_value was an unsigned long constant. On the other hand
this probably won't even compile:

typedef enum
{
    big_value= 0xFFFFFFFF;

Quote:
} big_enum_t;

I'm aware of the packed facility of some compilers. GNU C has a packed
attribute which is the same facility with different syntax. This doesn't
give the programmer as much control. The compiler could choose either
unsigned char or signed char for:

typedef packed enum { RED, GREEN, BLUE} primary_colours;

In some architectures eg PowerPC, unsigned char is more efficient in terms
of code space and time that signed char.

Does anyone know of any prior art along the lines I'm suggesting? General
comments and experiences with such prior art if it exists are welcome.
--



Tue, 22 Apr 2003 03:52:53 GMT  
 enums

Quote:

> I have always been of the opinion that enums in C are deficient
> because the compatible integer type is implementation defined.

? Type "int" itself has implementation-defined aspects.

Quote:
> This has two distinct serious disadvantages:
> 1. they cannot be used for the basic elements of large data structures where
> space is at a premium,
> 2. they cannot be portably used where the enumeration constants are outside
> the range of an int.

Requiring any specific type for enums would not help conserve space,
whereas allowing the implementation to use a variety of integer types
does help conserve space.

Enums are not mean to provide a portable external data format.

Quote:
> typedef unsigned long enum

Ideas along these lines have been suggested before.  Implement it,
gather feedback on how useful it actually is (which has been questioned
in the past), and if it proves to have sufficient utility it might be
adopted for some future revision of the C standard.
--



Wed, 23 Apr 2003 00:07:53 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. enums indexed by enums (again)

2. enums question

3. Converting Enums 2 String

4. Cannot access enums because compiler says they are protected

5. Problem boxing enums as attribute properties

6. Accessing enums in a different assembly

7. Enums and Intellisense in VS.NET using Managed C++ extensions

8. const pointer arithmetic in consts & enums

9. Enums via reflection problem

10. endless enums...

11. A question concerning enums

12. How to enumerate enums?

 

 
Powered by phpBB® Forum Software