This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Warning about int<-->enum conversions? (Gabriel Dos Reis)  wrote on 08.07.04 in <>:

> (This is a recurrent theme for C++ who has tried to improve
> over the C situation.  In C, you can have both implicit
> conversions.  C++ says that you can't go from int to enums
> implicitly -- a first step to have proper behaving enums.
> Some people firmly believe that C is right and it is an endless
> source of complains, because most C programs (and headers!)
> just think of enums and ints as interchangeable.  And I can't blame
> them.  The language definition does everything in that
> direction.  I don't even imagine what would happen if every
> use of enums in context of integers should be decorated
> by an explicit cast)

There's a second difference between C and C++ that's important in this  

C++ has given us "const int x = 17*4;" so that we can actually use x, for  
example, to size an array ("double a[x];").

In C, you either have to use a #define - unscoped, need to put in explicit  
parentheses, and so on - or your *only* other option is something like  
"enum { x = 17*4 };". (And then you are still limited to ints. No double  
or long long constants this way, for example.)

It's fairly easy to have enums incompatible with ints if your language has  
true constants - like C++, or Pascal, or (I presume) Ada. If, however, all  
your true constants *are* enums (or if you want to be compatible with a  
dialect where that is true), having them incompatible with ints is a  
really, really bad idea.

MfG Kai

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]