This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Warning about int<-->enum conversions?
- From: kaih at khms dot westfalen dot de (Kai Henningsen)
- To: gcc at gcc dot gnu dot org
- Date: 08 Jul 2004 19:47:00 +0200
- Subject: Re: Warning about int<-->enum conversions?
- Comment: Unsolicited commercial mail will incur an US$100 handling fee per received mail.
- Organization: Organisation? Me?! Are you kidding?
- References: <20040708142931.CB6B1F2EBC@nile.gnat.com> <20040708142931.CB6B1F2EBC@nile.gnat.com> <32810.::ffff:128.194.146.36.1089297660.squirrel@webmail.nerim.net>
gdr@acm.org (Gabriel Dos Reis) wrote on 08.07.04 in <32810.::ffff:128.194.146.36.1089297660.squirrel@webmail.nerim.net>:
> (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
context.
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