Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #162932
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: redeclaration of enumerators? |
| Date | 2021-10-02 12:21 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <sj9bqp$epd$1@dont-email.me> (permalink) |
| References | (9 earlier) <87tui2j1m3.fsf@nosuchdomain.example.com> <sj44gi$odk$1@dont-email.me> <bf3ac5ed-f44b-4f8d-9ffe-ad9583ade948n@googlegroups.com> <sj6csf$dvm$1@dont-email.me> <8735pkihrn.fsf@nosuchdomain.example.com> |
On 01/10/2021 22:58, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 30/09/2021 16:36, Malcolm McLean wrote:
>>> On Thursday, 30 September 2021 at 11:45:50 UTC+1, David Brown wrote:
>>>>
>>>> But I think that in most cases, it is not at all helpful to think about
>>>> the underlying integer values. You get cleaner, safer, more portable
>>>> code if you treat the enumeration constants as being abstract symbols
>>>> that are members of the particular type without any other semantics or
>>>> meaning.
>>>>
>>> I agree, but there are some snags. One is if you wish to print out
>>> the enum for debug purposes. It's easy enough to write printf("light
>>> %d\n", (int) lightenumval); If you have to set up an enum to string
>>> function then it's a bit clearer, but it's a huge hassle for debug.
>>> The other snag is that often you want to use the enum to index into a table
>>> of some sort. Again, you can set up a system by writing a switch, but that's
>>> likely slower and more code.
>>
>> These are not "snags" - they are cases where the underlying integer
>> value of the enumeration constants is sometimes helpful due to the
>> limitations of the language. Eventually, C++ will get some decent
>> compile-time reflection capabilities that will allow you to generate
>> strings from enumeration constants simply and easily, without repetition
>> in the code (and without complicated X-macros).
>
> Even for C++ scoped enumerations, each enumerator has a well defined
> numeric value that can be specified in the declaration and accessed by
> casting to an integer type. This is part of the language-defined
> semantics of the feature, not something that the language incorrectly
> fails to hide.
>
Yes, I know these are well-defined as part of the C++ language. I don't
think it is always a good thing that these are user-visible, as for many
uses I would prefer enumeration constants to be purely abstract symbols.
Obviously you can ignore the underlying integer value and pretend they
are just symbols. But people often write code that relies on their
integer nature (such as incrementing a variable of an enumerated type
representing a state, rather than giving the new state explicitly) in a
way that IMHO gives lazier, but poorer code. And the fact that the
language treats enumeration types as a kind of integer, and enumeration
constants as a type of integer constant, means it is far too easy to
make mistakes in the code that are not errors according to the language,
and it also limits the compiler's analysis and optimisations.
C++ scoped enums help with some of that, and compiler flags can also
improve the safety and efficiency of enumerations in some situations.
The real limitation of C and C++ in this area, IMHO, is that there are
no reflection capabilities in the language at the moment. There is
nothing equivalent to Ada's enumeration operators and attributes
"Image", "Value", "First", "Last", "Succ", etc. Several of the cases
mentioned in this thread for when the underlying integer values are
useful, is precisely because of this limitation.
> I've personally found it annoying that a scoped enum can't be used as an
> array index. (Ada's enumeration types are distinct types, more strongly
> typed than C++'s scoped enums, and they can be used as array
> indices. Of course neither C nor C++ is Ada, nor should they be.)
>
I agree entirely. I also agree that Ada is a different language with
different balances from both C and C++ - however, languages can take
inspiration from each other and copy or adapt useful features. (I don't
think reflection capabilities are something that C should adopt here,
but they /are/ appropriate for C++.)
> It's difficult to extend a weakly typed feature like C's enum and make
> it reasonably type-safe without breaking things. If C were to add a
> more strongly typed enumeration feature, it would probably make sense to
> adopt something very similar to what C++ has already done.
>
Yes. But it is possible to go further than C++ has.
>> In the meantime, C99, and C++ with gcc extensions (unfortunately not
>> standard), let you write:
>>
>>
>> typedef enum Colours { red, green, blue } Colours;
>>
>> extern const char * const colour_names[];
>> const char * const colour_names[] = {
>> [red] = "red",
>> [green] = "green",
>> [blue] = "blue",
>> };
>>
>> This does not use or rely on underlying integer values in any way -
>> there is no need even to have the same order in the array definition as
>> in the enumeration.
>>
>> For convenience (and for standard C++) you might define such an array
>> without designated initialisers, but you are still trying to view the
>> enumeration elements as abstract symbols, not as merely names for
>> integer constants.
>
> If you're going to be doing this a lot, it probably makes sense to
> generate the table automatically.
>
Yes. I mentioned "X macros", which are a common way to handle such
things. External preprocessing can also sometimes be useful, especially
if the enumeration contents are from an external source. But it would
be nice to be able to handle this all more naturally within the language
(and I'm thinking C++ more than C) in a less manual fashion.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
redeclaration of enumerators? Thiago Adams <thiago.adams@gmail.com> - 2021-09-27 10:39 -0700
Re: redeclaration of enumerators? John Bode <jfbode1029@gmail.com> - 2021-09-28 16:21 -0500
Re: redeclaration of enumerators? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-09-28 14:57 -0700
Re: redeclaration of enumerators? Thiago Adams <thiago.adams@gmail.com> - 2021-09-28 15:23 -0700
Re: redeclaration of enumerators? David Brown <david.brown@hesbynett.no> - 2021-09-29 15:23 +0200
Re: redeclaration of enumerators? Mark Bluemel <mark.bluemel@gmail.com> - 2021-09-30 01:15 -0700
Re: redeclaration of enumerators? Bart <bc@freeuk.com> - 2021-09-28 23:31 +0100
Re: redeclaration of enumerators? David Brown <david.brown@hesbynett.no> - 2021-09-29 15:36 +0200
Re: redeclaration of enumerators? Bart <bc@freeuk.com> - 2021-09-29 15:03 +0100
Re: redeclaration of enumerators? David Brown <david.brown@hesbynett.no> - 2021-09-29 16:46 +0200
Re: redeclaration of enumerators? Bart <bc@freeuk.com> - 2021-09-29 16:08 +0100
Re: redeclaration of enumerators? David Brown <david.brown@hesbynett.no> - 2021-09-29 20:14 +0200
Re: redeclaration of enumerators? Thiago Adams <thiago.adams@gmail.com> - 2021-09-29 13:37 -0700
Re: redeclaration of enumerators? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-09-29 18:25 -0700
Re: redeclaration of enumerators? David Brown <david.brown@hesbynett.no> - 2021-09-30 12:45 +0200
Re: redeclaration of enumerators? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-09-30 07:36 -0700
Re: redeclaration of enumerators? David Brown <david.brown@hesbynett.no> - 2021-10-01 09:20 +0200
Re: redeclaration of enumerators? Bart <bc@freeuk.com> - 2021-10-01 11:24 +0100
Re: redeclaration of enumerators? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-10-01 04:49 -0700
Re: redeclaration of enumerators? Bart <bc@freeuk.com> - 2021-10-01 13:06 +0100
Re: redeclaration of enumerators? Tony Oliver <guinness.tony@gmail.com> - 2021-10-01 05:24 -0700
Re: redeclaration of enumerators? Bart <bc@freeuk.com> - 2021-10-01 13:32 +0100
Re: redeclaration of enumerators? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-10-01 05:32 -0700
Re: redeclaration of enumerators? Bart <bc@freeuk.com> - 2021-10-01 13:43 +0100
Re: redeclaration of enumerators? Mark Bluemel <mark.bluemel@gmail.com> - 2021-10-01 06:13 -0700
Re: redeclaration of enumerators? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-10-01 06:29 -0700
Re: redeclaration of enumerators? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-10-01 15:30 +0100
Re: redeclaration of enumerators? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-10-01 08:28 -0700
Re: redeclaration of enumerators? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-10-01 17:08 +0100
Re: redeclaration of enumerators? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-10-01 15:12 -0700
Re: redeclaration of enumerators? David Brown <david.brown@hesbynett.no> - 2021-10-01 15:37 +0200
Re: redeclaration of enumerators? Bart <bc@freeuk.com> - 2021-10-01 15:30 +0100
Re: redeclaration of enumerators? David Brown <david.brown@hesbynett.no> - 2021-10-01 19:12 +0200
Re: redeclaration of enumerators? Bart <bc@freeuk.com> - 2021-10-01 19:06 +0100
Re: redeclaration of enumerators? David Brown <david.brown@hesbynett.no> - 2021-10-01 15:32 +0200
Re: redeclaration of enumerators? Bart <bc@freeuk.com> - 2021-10-01 15:38 +0100
Re: redeclaration of enumerators? Bart <bc@freeuk.com> - 2021-10-01 17:40 +0100
Re: redeclaration of enumerators? Bart <bc@freeuk.com> - 2021-10-02 12:16 +0100
Re: redeclaration of enumerators? David Brown <david.brown@hesbynett.no> - 2021-10-02 15:34 +0200
Re: redeclaration of enumerators? Bart <bc@freeuk.com> - 2021-10-02 15:16 +0100
Re: redeclaration of enumerators? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-01 13:58 -0700
Re: redeclaration of enumerators? Richard Damon <Richard@Damon-Family.org> - 2021-10-01 17:21 -0400
Re: redeclaration of enumerators? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-10-01 14:56 -0700
Re: redeclaration of enumerators? David Brown <david.brown@hesbynett.no> - 2021-10-02 12:21 +0200
Re: redeclaration of enumerators? scott@slp53.sl.home (Scott Lurndal) - 2021-09-30 14:47 +0000
Re: redeclaration of enumerators? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-09-30 10:10 -0700
Re: redeclaration of enumerators? David Brown <david.brown@hesbynett.no> - 2021-10-01 09:24 +0200
Re: redeclaration of enumerators? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-09-29 12:43 -0700
Re: redeclaration of enumerators? Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-09-29 03:40 +0000
csiph-web