Re: enum safety



Ian Collins wrote:

Sard wrote:
Hi,

Page 39 K&R2 says

'Although variables of enum types may be declared, compilers need not
check that what you store in such a variable is a valid value for the
enumeration'

This is why (in my opinion) enums are horribly broken in C.
Not necessarily. For example, you can have an object which can contain
a number up to 10 as numbers, and 11, 12 and 13 with special meanings. E.g.
enum rank { ACE = 1, JACK = 11, QUEEN = 12, KING = 13 };
enum rank foo = 7;
(For example mbrtowc can return a number of bytes, or 0, (size_t)(-2) and
(size_t)(-1) with special meanings. Here an enum won't do it because
constants must fit in an int, but one might have an int-returning function
with similar semantics. Maybe using negative numbers:
enum err_code { OUT_OF_MEMORY = -2, IO_ERROR = -1, MAX = INT_MAX };

--
Army1987 (Replace "NOSPAM" with "email")
.



Relevant Pages

  • enum safety
    ... 'Although variables of enum types may be declared, compilers need not ...
    (comp.lang.c)
  • Collection of Enum with additional properties
    ... I was to track some additional properties around an enum I have - for ... I also need to be able to store and manage a collection of these ... EnumDescriptor objects... ... collection class by inheriting from the the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: enum safety
    ... 'Although variables of enum types may be declared, compilers need not ... check that what you store in such a variable is a valid value for the ...
    (comp.lang.c)
  • Re: BIT banging help
    ...  how many enum values can I store with a long? ... store as a bigint in sqlserver). ... I want to be able to AND and OR each permission enum value, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: how to parse an Enum Structure in vb.net
    ... Storing the names is something that should be considered in some cases, ... In other cases the names have no natural numbering at all, then it might be better to store the names. ... You can just as easily assign numbers to each enum value, i.e., ... Filibuster = 25 ...
    (microsoft.public.dotnet.languages.vb)