Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #162865
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: redeclaration of enumerators? |
| Date | 2021-09-29 15:23 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <sj1pd2$4f2$1@dont-email.me> (permalink) |
| References | <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com> <sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com> <467a5838-5a51-4b4b-8e01-c28969b69a65n@googlegroups.com> |
On 29/09/2021 00:23, Thiago Adams wrote:
> On Tuesday, September 28, 2021 at 6:58:00 PM UTC-3, Keith Thompson wrote:
>> John Bode <jfbod...@gmail.com> writes:
>>> On 9/27/21 12:39 PM, Thiago Adams wrote:
>>>> I was wondering if we have some reason to
>>>> disallow redeclaration of identical (name/value) enumerators.
>>>> enum E1 { A = 1 };
>>>> enum E2 { A = 1 };
>>>
>>> Unlike structs and unions, each enum type is not its own
>>> namespace and enumeration constants belong to the "ordinary
>>> identifiers" namespace.
>>>
>>> The question is how you would disambiguate the two definitions
>>> of A - there's no equivalent to the '.' or '->' operators for
>>> enums. There's no way to easily say "I'm talking about the A
>>> defined for enum E1, not the A defined for enum E2".
>>>
>>> Can't rely on type inference in assignments; enums are just an
>>> incredibly weak abstraction in C. There's no range checking
>>> such that you can only assign one of the defined enumeration
>>> constants to an object of that enum type.
>>>
>>> You'd either need to introduce a C++-style scoping operator like
>>> E1::A or E2::A, or you'd need to radically revamp and strengthen
>>> enum semantics. And in the process you'd likely lose some capability
>>> that people find incredibly useful, such as bitwise-ORing enumeration
>>> constants together.
>>>
>>> enums in C are little more than a way to create symbolic constants for
>>> integer values. They are not really *enumerated types* like you
>>> find in other languages.
>> I think the suggestion is to allow duplicate names only if both the name
>> and the value happen to match. Thiago's example:
>> enum E1 { A = 1 };
>> enum E2 { A = 1 };
>> would be valid, but changing one of the values:
>> enum E1 { A = 1 };
>> enum E2 { A = 2 };
>
> Yes. (This is allowed in defines for instance)
> #define A 1
> #define A 1
>
> (
> I said: "But we already have a problem today:""
> I forgot that in C enumerators are ints. They don't have
> the enum type. A is int not enum E1 or enum E2.
> So .. this is another reason to allow it?
> )
>
> It may be tedious to duplicate enum values or create defines
> for the enum value.. so I was thinking.
>
> enum ALL { A, B , C };
> enum E { extern A };
>
> The A as it is (same value) is used in E as well.
> The value name/value "is imported". The type of A is int.
>
> So what is the objective?
>
> The enum can be used as "set of possible values" and
> the same enumerators are reused in different sets.
>
> The advantage of enum over constants is that these sets
> can be verified in compiler time in switchs or for valid
> assignment or comparisons and make the code clear given a type.
>
> I my code this would be useful because I implement polymorphism
> in C using "tags" inside structs. These tags are sets of types I if
> I forgot one switch case my code is wrong. I have/use a ALLTAGS enum
> because I need unique tags . (enum ensures that)
>
enums are, in my experience, mostly used for two things. One is when
you want to keep a collection of related flags, states, options, etc.,
in a type :
typedef enum States {
state_startup, state_idle, state_turning_on,
state_on, state_turning_off
} States;
You have a new type, "States" or "enum States" (according to
preference), and values for it. In C++ you'd likely want a scoped enum,
but C does not have them - so just as for namespaces and other nested
naming that C does not have, you use a naming convention for the
enumeration constants. Prefixing with the enumeration name is a common
method. There will not be duplicates or conflicts.
The other major use of enums is simply as named int constants :
enum { no_of_whatsits = 40 };
(In C++, you'd probably just use "const int no_of_whatsits = 40;".)
Here you are only interested in the constants, not the enumeration
types, and there is no particular benefit in keeping them together
inside one enum or splitting them into different enums. There is no
reason for duplications.
So really, I don't see the problem - I can't say I have noticed it in my
own coding.
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