开发者

Is using enums safe in all compilers?

开发者 https://www.devze.com 2023-02-24 17:07 出处:网络
In a discussion, a colleague told me that he never uses enum because he experienced that some C-compilers don\'t cope with the enum statement correctly.

In a discussion, a colleague told me that he never uses enum because he experienced that some C-compilers don't cope with the enum statement correctly.

He couldn't remember which compiler exactly had problems but among the problems, there were errors when doing something like

enum my_enum{
  my_enum_first  = 5;
  my_enum_second = 10;
};

i.e. initializing enum values instead of letting the compiler do the automatic assignment. Another one was that the compiler decides for itself how big the enum is and therefore you could have unpredictable behavior for sizeof my_enum when compiling your code under various platforms.

To get around that, he told me to better use #defines to define the constant elements. But especially for using doxygen it's quite handy to have an enum (e.g. as function parameter) because in the generated documentation, you could simply click on my_enum and directly jump to the description of my_enum.

Another example would be code completion, where your IDE tells you what you could specify as valid parameters for functions. I know that – as long as you're compiling the code as C-code – that there's no type-safety (i.e. I could also specify 5 instead of my_enum_first), so the use of an enum seems to be a more c开发者_开发技巧osmetic thing.

The question is: do you know any compilers that have limitations regarding the usage of enum?


Edit 1:

Regarding the environment: we are developing for various embedded platforms, so there could also be a compiler for some obscure micro-controller...


Edit 2:

He could tell me that the KEIL C51 compiler didn't play well with enums. Are there any experiences with current versions of the C51 compiler?


Compilers are free to choose the size of an enum based on its range of possible values. This only really becomes an issue if you're exposing enums in your API, and users of your code may be using a different compiler or build options.

In this case, confusion can be caused by the calling code passing in a 16-bit value, for example, and the receiving code expecting it to be 32 bits. If the top 16 bits of the passed-in value are left uninitialized, then bad things will happen.

You can work around this kind of issue by including a dummy entry in your enum to enforce a minimum size.

For example:

typedef enum {
    FirstValue = 12,
    SecondValue = 25,

    DummyValue = 65536  // force enum to be greater than 16 bits
} MyEnum;


I'm pretty sure that a compiler that doesn't play nice with enum is an invalid compiler - enum is specified in the standard, so a failure to implement it means the compiler shouldn't technically be used to compile C (For the record, the scope of enumeration types is discussed in 6.2.1 and defined as a type in 6.2.5 (of C99), so one would assume that it's a valid part of the standard from thereon in)!
So no, I don't know of any such compilers.

0

精彩评论

暂无评论...
验证码 换一张
取 消