Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"He don't know me vewy well, DO he?" -- Bugs Bunny


devel / comp.lang.c / redeclaration of enumerators?

SubjectAuthor
* redeclaration of enumerators?Thiago Adams
`* Re: redeclaration of enumerators?John Bode
 +* Re: redeclaration of enumerators?Keith Thompson
 |+* Re: redeclaration of enumerators?Thiago Adams
 ||+- Re: redeclaration of enumerators?David Brown
 ||`- Re: redeclaration of enumerators?Mark Bluemel
 |`* Re: redeclaration of enumerators?Bart
 | `* Re: redeclaration of enumerators?David Brown
 |  `* Re: redeclaration of enumerators?Bart
 |   +* Re: redeclaration of enumerators?David Brown
 |   |`* Re: redeclaration of enumerators?Bart
 |   | `* Re: redeclaration of enumerators?David Brown
 |   |  +- Re: redeclaration of enumerators?Thiago Adams
 |   |  `* Re: redeclaration of enumerators?Keith Thompson
 |   |   `* Re: redeclaration of enumerators?David Brown
 |   |    +* Re: redeclaration of enumerators?Malcolm McLean
 |   |    |`* Re: redeclaration of enumerators?David Brown
 |   |    | +* Re: redeclaration of enumerators?Bart
 |   |    | |+* Re: redeclaration of enumerators?Malcolm McLean
 |   |    | ||`* Re: redeclaration of enumerators?Bart
 |   |    | || +* Re: redeclaration of enumerators?Tony Oliver
 |   |    | || |`- Re: redeclaration of enumerators?Bart
 |   |    | || +* Re: redeclaration of enumerators?Malcolm McLean
 |   |    | || |+* Re: redeclaration of enumerators?Bart
 |   |    | || ||+- Re: redeclaration of enumerators?Mark Bluemel
 |   |    | || ||`- Re: redeclaration of enumerators?Malcolm McLean
 |   |    | || |`* Re: redeclaration of enumerators?Ben Bacarisse
 |   |    | || | `* Re: redeclaration of enumerators?Malcolm McLean
 |   |    | || |  `* Re: redeclaration of enumerators?Ben Bacarisse
 |   |    | || |   `- Re: redeclaration of enumerators?Malcolm McLean
 |   |    | || `* Re: redeclaration of enumerators?David Brown
 |   |    | ||  `* Re: redeclaration of enumerators?Bart
 |   |    | ||   `* Re: redeclaration of enumerators?David Brown
 |   |    | ||    `- Re: redeclaration of enumerators?Bart
 |   |    | |+* Re: redeclaration of enumerators?David Brown
 |   |    | ||`* Re: redeclaration of enumerators?Bart
 |   |    | || `- Re: redeclaration of enumerators?Bart
 |   |    | |`* Re: redeclaration of enumerators?Bart
 |   |    | | `* Re: redeclaration of enumerators?David Brown
 |   |    | |  `- Re: redeclaration of enumerators?Bart
 |   |    | `* Re: redeclaration of enumerators?Keith Thompson
 |   |    |  +- Re: redeclaration of enumerators?Richard Damon
 |   |    |  +- Re: redeclaration of enumerators?Malcolm McLean
 |   |    |  `- Re: redeclaration of enumerators?David Brown
 |   |    `* Re: redeclaration of enumerators?Scott Lurndal
 |   |     +- Re: redeclaration of enumerators?Malcolm McLean
 |   |     `- Re: redeclaration of enumerators?David Brown
 |   `- Re: redeclaration of enumerators?Andrey Tarasevich
 `- Re: redeclaration of enumerators?Branimir Maksimovic

Pages:12
redeclaration of enumerators?

<a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18417&group=comp.lang.c#18417

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a37:9a87:: with SMTP id c129mr1235905qke.191.1632764389684;
Mon, 27 Sep 2021 10:39:49 -0700 (PDT)
X-Received: by 2002:a0c:db0a:: with SMTP id d10mr865914qvk.28.1632764389491;
Mon, 27 Sep 2021 10:39:49 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 27 Sep 2021 10:39:49 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.249.78; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.249.78
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
Subject: redeclaration of enumerators?
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Mon, 27 Sep 2021 17:39:49 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 64
 by: Thiago Adams - Mon, 27 Sep 2021 17:39 UTC

I was wondering if we have some reason to
disallow redeclaration of identical (name/value) enumerators.

enum E1 { A = 1 };
enum E2 { A = 1 };

The enum type in this case could be used to indicate the
set type while the enumerators are just constant of type int
that can be converted to enum set.

The type of the constant that is ambiguous it can be E1 or E2.
maybe this is the problem...

But we already have a problem today:

#include <stdio.h>
enum E1 {
A = 1
};

#define typename(x) \
_Generic((x),\
unsigned int: "int",\
enum E1 : "enum E1", \
default: "other")

int main() {
printf("%s %s", typename(A), typename(1));
}

this is the gcc error
error: '_Generic' specifies two compatible types

this compiles:

#include <stdio.h>

enum E1 { A1 = 1 };
enum E2 { A2 = 1 };

#define typename(x) \
_Generic((x),\
enum E1 : "enum E1", \
enum E2 : "enum E2", \
default: "other")

int main() {
printf("%s %s", typename(A1), typename(A2));
} and it prints
other other

---
(changing to)

#define typename(x) \
_Generic((x),\
enum E1 : "enum E1", \
int : "int", \
default: "other")

prints
int int

Re: redeclaration of enumerators?

<sj010b$715$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18427&group=comp.lang.c#18427

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jfbode1...@gmail.com (John Bode)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Tue, 28 Sep 2021 16:21:13 -0500
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <sj010b$715$1@dont-email.me>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 28 Sep 2021 21:21:15 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f8f786c28a92ba52d2ecb9f2c77cc8bb";
logging-data="7205"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX190D/Dazw07p1ksQXp3R8XM"
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0)
Gecko/20100101 Thunderbird/78.14.0
Cancel-Lock: sha1:pidt41jd6rQnG/qgEOmuzxGl5j4=
In-Reply-To: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
Content-Language: en-US
 by: John Bode - Tue, 28 Sep 2021 21:21 UTC

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.

Re: redeclaration of enumerators?

<8735pojrc3.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18428&group=comp.lang.c#18428

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Tue, 28 Sep 2021 14:57:48 -0700
Organization: None to speak of
Lines: 57
Message-ID: <8735pojrc3.fsf@nosuchdomain.example.com>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="55216b196792f48554ed3c16d82f8c24";
logging-data="14153"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/SlHMpLO7RJs1f+l2/X/lN"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:Sp3CGspt0VXUf44DWbZfjHswY0Y=
sha1:PYTJZ5wNlq28ZmaLf1jpx2eh4fg=
 by: Keith Thompson - Tue, 28 Sep 2021 21:57 UTC

John Bode <jfbode1029@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 };

would result in a constraint violation. (I presume the same would apply
if the values are chosen implicitly.)

There's no fundamental reason this couldn't be done, but I don't think
it's useful enough to justify a language change. If you want two enum
types to share values, you can define them as a single type.

(C++ does have an "enum class" feature which, among other things, allows
for duplicate names, but then you always need to refer to "E1::A"
whether it's ambiguous or not.)

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: redeclaration of enumerators?

<467a5838-5a51-4b4b-8e01-c28969b69a65n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18429&group=comp.lang.c#18429

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:12:: with SMTP id a18mr8305567qtg.157.1632867813408;
Tue, 28 Sep 2021 15:23:33 -0700 (PDT)
X-Received: by 2002:a37:be87:: with SMTP id o129mr2499239qkf.213.1632867813159;
Tue, 28 Sep 2021 15:23:33 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Tue, 28 Sep 2021 15:23:32 -0700 (PDT)
In-Reply-To: <8735pojrc3.fsf@nosuchdomain.example.com>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.249.78; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.249.78
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <467a5838-5a51-4b4b-8e01-c28969b69a65n@googlegroups.com>
Subject: Re: redeclaration of enumerators?
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Tue, 28 Sep 2021 22:23:33 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 73
 by: Thiago Adams - Tue, 28 Sep 2021 22:23 UTC

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)

Re: redeclaration of enumerators?

<sj053r$6c4$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18430&group=comp.lang.c#18430

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Tue, 28 Sep 2021 23:31:16 +0100
Organization: A noiseless patient Spider
Lines: 61
Message-ID: <sj053r$6c4$1@dont-email.me>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 28 Sep 2021 22:31:23 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="df84eaf1d188b1c0c8059190bf4dd358";
logging-data="6532"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/WCR1voWpwAruy5MTQeZILYXbPj67FHTU="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:+Rzjq9LW4sYCpfYMcM3dkpVZpos=
In-Reply-To: <8735pojrc3.fsf@nosuchdomain.example.com>
X-Antivirus-Status: Clean
Content-Language: en-GB
X-Antivirus: AVG (VPS 210928-8, 28/9/2021), Outbound message
 by: Bart - Tue, 28 Sep 2021 22:31 UTC

On 28/09/2021 22:57, Keith Thompson wrote:
> John Bode <jfbode1029@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 };
>
> would result in a constraint violation. (I presume the same would apply
> if the values are chosen implicitly.)
>
> There's no fundamental reason this couldn't be done, but I don't think
> it's useful enough to justify a language change. If you want two enum
> types to share values, you can define them as a single type.
>
> (C++ does have an "enum class" feature which, among other things, allows
> for duplicate names, but then you always need to refer to "E1::A"
> whether it's ambiguous or not.)

That's surprising. With stricter typing, you think it would figure out
that the A here:

enum E2 x = A;

needs to be E2::A.

Re: redeclaration of enumerators?

<TeR4J.59818$jm6.2396@fx07.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18432&group=comp.lang.c#18432

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx07.iad.POSTED!not-for-mail
Newsgroups: comp.lang.c
From: branimir...@gmail.com (Branimir Maksimovic)
Subject: Re: redeclaration of enumerators?
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me>
User-Agent: slrn/1.0.3 (Darwin)
Lines: 20
Message-ID: <TeR4J.59818$jm6.2396@fx07.iad>
X-Complaints-To: abuse@usenet-news.net
NNTP-Posting-Date: Wed, 29 Sep 2021 03:40:35 UTC
Organization: usenet-news.net
Date: Wed, 29 Sep 2021 03:40:35 GMT
X-Received-Bytes: 1155
 by: Branimir Maksimovic - Wed, 29 Sep 2021 03:40 UTC

On 2021-09-28, John Bode <jfbode1029@gmail.com> wrote:
> 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.
namespace is not needed for grown up men/women.
Just put prefix and voila :P

--

7-77-777
Evil Sinner!

Re: redeclaration of enumerators?

<sj1pd2$4f2$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18433&group=comp.lang.c#18433

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Wed, 29 Sep 2021 15:23:45 +0200
Organization: A noiseless patient Spider
Lines: 104
Message-ID: <sj1pd2$4f2$1@dont-email.me>
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>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 29 Sep 2021 13:23:46 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="95fcff34af4c4b265731a971b9f45c41";
logging-data="4578"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX193RvfsTeSX8PLTiT/JgrkHeBwMCq2KKGs="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:o+hJzQJOg+FwYksJ+A/ilOyBPTM=
In-Reply-To: <467a5838-5a51-4b4b-8e01-c28969b69a65n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Wed, 29 Sep 2021 13:23 UTC

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.

Re: redeclaration of enumerators?

<sj1q5a$a7i$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18434&group=comp.lang.c#18434

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Wed, 29 Sep 2021 15:36:41 +0200
Organization: A noiseless patient Spider
Lines: 82
Message-ID: <sj1q5a$a7i$1@dont-email.me>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 29 Sep 2021 13:36:42 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="95fcff34af4c4b265731a971b9f45c41";
logging-data="10482"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Uk7xc6I1Evbi8FwLFqCkvZc4h+tdVi4w="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:iBUqTz1rzYEQJbhj8+BnzJ4Uif4=
In-Reply-To: <sj053r$6c4$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Wed, 29 Sep 2021 13:36 UTC

On 29/09/2021 00:31, Bart wrote:
> On 28/09/2021 22:57, Keith Thompson wrote:
>> John Bode <jfbode1029@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 };
>>
>> would result in a constraint violation.  (I presume the same would apply
>> if the values are chosen implicitly.)
>>
>> There's no fundamental reason this couldn't be done, but I don't think
>> it's useful enough to justify a language change.  If you want two enum
>> types to share values, you can define them as a single type.
>>
>> (C++ does have an "enum class" feature which, among other things, allows
>> for duplicate names, but then you always need to refer to "E1::A"
>> whether it's ambiguous or not.)
>
> That's surprising. With stricter typing, you think it would figure out
> that the A here:
>
>     enum E2 x = A;
>
> needs to be E2::A.
>

It is not remotely surprising - in order to be able to use identifiers
within a nested code structure (I'm using that as a general term, not
just "struct") of some sort, you need to be "inside" the structure, in
the scope of a "using" clause, or you need to give the qualified name.

It would be /convenient/ to have the qualified name handled
automatically here. That would make scoped enums a lot easier in use
and avoid unnecessary wordiness. There is a proposal in the works (I
can't remember off-hand if it got added to the standard) to allow "using
enum E2;" to bring the "E2::" enumerators into scope. That is not ideal
either, but still an improvement in usability.

Maybe new parsing rules for C++ can be figured out that would allow "E2
X = A;", or at least "E2 x { A };". I don't see how it could easily be
done, however, without a lot of complications and side-effects. This
isn't an ad-hoc language where you can just make up the rules as you go
along without a care of how they affect other parts of the language.

Re: redeclaration of enumerators?

<sj1ro3$ng2$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18435&group=comp.lang.c#18435

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Wed, 29 Sep 2021 15:03:39 +0100
Organization: A noiseless patient Spider
Lines: 107
Message-ID: <sj1ro3$ng2$1@dont-email.me>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 29 Sep 2021 14:03:47 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="df84eaf1d188b1c0c8059190bf4dd358";
logging-data="24066"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18FZNDkiI7kClMdBW24S4nKe6Q5yw0GFJE="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:/jfQ7e44LApX0mFw3DIKjLEpr9M=
In-Reply-To: <sj1q5a$a7i$1@dont-email.me>
X-Antivirus-Status: Clean
Content-Language: en-GB
X-Antivirus: AVG (VPS 210929-2, 29/9/2021), Outbound message
 by: Bart - Wed, 29 Sep 2021 14:03 UTC

On 29/09/2021 14:36, David Brown wrote:
> On 29/09/2021 00:31, Bart wrote:
>> On 28/09/2021 22:57, Keith Thompson wrote:
>>> John Bode <jfbode1029@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 };
>>>
>>> would result in a constraint violation.  (I presume the same would apply
>>> if the values are chosen implicitly.)
>>>
>>> There's no fundamental reason this couldn't be done, but I don't think
>>> it's useful enough to justify a language change.  If you want two enum
>>> types to share values, you can define them as a single type.
>>>
>>> (C++ does have an "enum class" feature which, among other things, allows
>>> for duplicate names, but then you always need to refer to "E1::A"
>>> whether it's ambiguous or not.)
>>
>> That's surprising. With stricter typing, you think it would figure out
>> that the A here:
>>
>>     enum E2 x = A;
>>
>> needs to be E2::A.
>>
>
> It is not remotely surprising - in order to be able to use identifiers
> within a nested code structure (I'm using that as a general term, not
> just "struct") of some sort, you need to be "inside" the structure, in
> the scope of a "using" clause, or you need to give the qualified name.

Yeah, you're right. It would be tricky anyway, because it can't be
sorted using normal name resolution rules, since it relies on type info
which may not be dealt with until a subsequent stage.

But also, you could have this:

enum E2 {A=1, B, C};
enum E2 A, B;

A = E2::C
B = A;

Is the RHS the variable A (which contains 3), or is it intended to be
E2::A, which is 1?

(This makes me feel better about my own stuff, which can have 'closed'
enums like C, but they need qualifying too:

enum colours = (red, green, blue)
enum lights = (red, amber, green)

colours A = colours.green # can't use A = green)

> Maybe new parsing rules for C++ can be figured out that would allow "E2
> X = A;", or at least "E2 x { A };". I don't see how it could easily be
> done, however, without a lot of complications and side-effects. This
> isn't an ad-hoc language where you can just make up the rules as you go
> along without a care of how they affect other parts of the language.

I don't make up ad-hoc languages; they have to work. You can only cut a
few minor corners.

But I understand parsing C++ can requires some ingenuity anyway, because
the language is so complex and with unexpected and ambiguous
interactions between features.

Re: redeclaration of enumerators?

<sj1u8e$bds$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18436&group=comp.lang.c#18436

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Wed, 29 Sep 2021 16:46:38 +0200
Organization: A noiseless patient Spider
Lines: 127
Message-ID: <sj1u8e$bds$1@dont-email.me>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me>
<sj1ro3$ng2$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 29 Sep 2021 14:46:39 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="95fcff34af4c4b265731a971b9f45c41";
logging-data="11708"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/SUCA3lY/u47V7t1q3iyuYRtPh9+rt7O0="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:nzkF+oKpiqnxZ6u0sTr0tp3IsgA=
In-Reply-To: <sj1ro3$ng2$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Wed, 29 Sep 2021 14:46 UTC

On 29/09/2021 16:03, Bart wrote:
> On 29/09/2021 14:36, David Brown wrote:
>> On 29/09/2021 00:31, Bart wrote:
>>> On 28/09/2021 22:57, Keith Thompson wrote:
>>>> John Bode <jfbode1029@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 };
>>>>
>>>> would result in a constraint violation.  (I presume the same would
>>>> apply
>>>> if the values are chosen implicitly.)
>>>>
>>>> There's no fundamental reason this couldn't be done, but I don't think
>>>> it's useful enough to justify a language change.  If you want two enum
>>>> types to share values, you can define them as a single type.
>>>>
>>>> (C++ does have an "enum class" feature which, among other things,
>>>> allows
>>>> for duplicate names, but then you always need to refer to "E1::A"
>>>> whether it's ambiguous or not.)
>>>
>>> That's surprising. With stricter typing, you think it would figure out
>>> that the A here:
>>>
>>>      enum E2 x = A;
>>>
>>> needs to be E2::A.
>>>
>>
>> It is not remotely surprising - in order to be able to use identifiers
>> within a nested code structure (I'm using that as a general term, not
>> just "struct") of some sort, you need to be "inside" the structure, in
>> the scope of a "using" clause, or you need to give the qualified name.
>
> Yeah, you're right. It would be tricky anyway, because it can't be
> sorted using normal name resolution rules, since it relies on type info
> which may not be dealt with until a subsequent stage.
>
> But also, you could have this:
>
>    enum E2 {A=1, B, C};
>    enum E2 A, B;
>
>        A = E2::C
>        B = A;
>
> Is the RHS the variable A (which contains 3), or is it intended to be
> E2::A, which is 1?
>
> (This makes me feel better about my own stuff, which can have 'closed'
> enums like C, but they need qualifying too:
>
>   enum colours = (red, green, blue)
>   enum lights  = (red, amber, green)
>
>   colours A = colours.green        # can't use A = green)
>
>> Maybe new parsing rules for C++ can be figured out that would allow "E2
>> X = A;", or at least "E2 x { A };".  I don't see how it could easily be
>> done, however, without a lot of complications and side-effects.  This
>> isn't an ad-hoc language where you can just make up the rules as you go
>> along without a care of how they affect other parts of the language.
>
>
> I don't make up ad-hoc languages; they have to work. You can only cut a
> few minor corners.
>
> But I understand parsing C++ can requires some ingenuity anyway, because
> the language is so complex and with unexpected and ambiguous
> interactions between features.
>

The aim is to /minimise/ the unexpected or ambiguous interactions (for
those that have learned the language) - that is one of the reasons why
changes are so hard to make in this area, as you have to get experts to
think of all the weirdest corner cases.

I checked on the "using enum" in C++. It was added in C++20, and lets
you write:

enum class colours { red, green, blue };
colours my_favourite_colour;

my_favourite_colour = colours::red;
using enum colours;
my_favourite_colour = blue;

Re: redeclaration of enumerators?

<sj1vgt$lfe$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18437&group=comp.lang.c#18437

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Wed, 29 Sep 2021 16:08:04 +0100
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <sj1vgt$lfe$1@dont-email.me>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me>
<sj1ro3$ng2$1@dont-email.me> <sj1u8e$bds$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 29 Sep 2021 15:08:13 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="df84eaf1d188b1c0c8059190bf4dd358";
logging-data="21998"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/RdOmYnHey6nVTr/rlmv40XOXYjowewb0="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:kNwHoYcC0DDT/d/auTqJm82HHQY=
In-Reply-To: <sj1u8e$bds$1@dont-email.me>
X-Antivirus-Status: Clean
Content-Language: en-GB
X-Antivirus: AVG (VPS 210929-2, 29/9/2021), Outbound message
 by: Bart - Wed, 29 Sep 2021 15:08 UTC

On 29/09/2021 15:46, David Brown wrote:
> On 29/09/2021 16:03, Bart wrote:

>> Yeah, you're right. It would be tricky anyway, because it can't be
>> sorted using normal name resolution rules, since it relies on type info
>> which may not be dealt with until a subsequent stage.
>>
>> But also, you could have this:
>>
>>    enum E2 {A=1, B, C};
>>    enum E2 A, B;
>>
>>        A = E2::C
>>        B = A;
>>
>> Is the RHS the variable A (which contains 3), or is it intended to be
>> E2::A, which is 1?

> I checked on the "using enum" in C++. It was added in C++20, and lets
> you write:
>
> enum class colours { red, green, blue };
> colours my_favourite_colour;
>
> my_favourite_colour = colours::red;
> using enum colours;
> my_favourite_colour = blue;

It took me a while to find something that supported C++20 (godbolt does)
to try it out.

How it works is that if you use:

using enum colours;

then you can't declare:

colours blue;

So the ambiguity I pointed out can't occur.

It stops you using 'int blue;' as well. In fact, all those enum names
become 'open', and you will get the same issues as you have in C with
clashes with other identifiers and other open enums.

(Except that this feature allowes you to restrict those open enums to a
particular scope.)

It doesn't help with my other example either: you can't use both:

using enum colours;
using enum lights;

as 'green' will clash; as well 'red', even though it they have the same
value.

Re: redeclaration of enumerators?

<sj2adj$emu$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18438&group=comp.lang.c#18438

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Wed, 29 Sep 2021 20:14:11 +0200
Organization: A noiseless patient Spider
Lines: 67
Message-ID: <sj2adj$emu$1@dont-email.me>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me>
<sj1ro3$ng2$1@dont-email.me> <sj1u8e$bds$1@dont-email.me>
<sj1vgt$lfe$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 29 Sep 2021 18:14:11 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3599a4fb10f838e6cd3474fe1440c280";
logging-data="15070"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9zNNdGaMsKnqrwskeyCF+qyzyb6M9QkI="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:tkrNAyl4YJ0mxUmsQwYkG6v/pvc=
In-Reply-To: <sj1vgt$lfe$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Wed, 29 Sep 2021 18:14 UTC

On 29/09/2021 17:08, Bart wrote:
> On 29/09/2021 15:46, David Brown wrote:
>> On 29/09/2021 16:03, Bart wrote:
>
>>> Yeah, you're right. It would be tricky anyway, because it can't be
>>> sorted using normal name resolution rules, since it relies on type info
>>> which may not be dealt with until a subsequent stage.
>>>
>>> But also, you could have this:
>>>
>>>     enum E2 {A=1, B, C};
>>>     enum E2 A, B;
>>>
>>>         A = E2::C
>>>         B = A;
>>>
>>> Is the RHS the variable A (which contains 3), or is it intended to be
>>> E2::A, which is 1?
>
>> I checked on the "using enum" in C++.  It was added in C++20, and lets
>> you write:
>>
>>     enum class colours { red, green, blue };
>>     colours my_favourite_colour;
>>
>>     my_favourite_colour = colours::red;
>>     using enum colours;
>>     my_favourite_colour = blue;
>
> It took me a while to find something that supported C++20 (godbolt does)
> to try it out.
>
> How it works is that if you use:
>
>    using enum colours;
>
> then you can't declare:
>
>    colours blue;
>
> So the ambiguity I pointed out can't occur.
>
> It stops you using 'int blue;' as well. In fact, all those enum names
> become 'open', and you will get the same issues as you have in C with
> clashes with other identifiers and other open enums.
>
> (Except that this feature allowes you to restrict those open enums to a
> particular scope.)

Of course. Typically, you'll have the "using enum" clause in quite a
small scope - just the block of code where you need it. It's not
something you'd have at file level.

>
> It doesn't help with my other example either: you can't use both:
>
>   using enum colours;
>   using enum lights;
>
> as 'green' will clash; as well 'red', even though it they have the same
> value.

With strong enumerations, you shouldn't even be thinking of them as
having an integer value - it doesn't make sense to compare the "value"
of a "colours" enumeration element with one from a "lights" enumeration.
That is a large part of the point of having better enumeration types
than were available in C.

Re: redeclaration of enumerators?

<sj2fkt$nd7$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18439&group=comp.lang.c#18439

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: andreyta...@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Wed, 29 Sep 2021 12:43:23 -0700
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <sj2fkt$nd7$1@dont-email.me>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me>
<sj1ro3$ng2$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 29 Sep 2021 19:43:25 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="4b610c3ea16f27c71aa1a693fc38de88";
logging-data="23975"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19OurHA2eiBK6NNFHkdyp+l"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:78DC6S5I8ntsR0LBO2CRjep4grE=
In-Reply-To: <sj1ro3$ng2$1@dont-email.me>
Content-Language: en-US
 by: Andrey Tarasevich - Wed, 29 Sep 2021 19:43 UTC

On 9/29/2021 7:03 AM, Bart wrote:
>>
>> It is not remotely surprising - in order to be able to use identifiers
>> within a nested code structure (I'm using that as a general term, not
>> just "struct") of some sort, you need to be "inside" the structure, in
>> the scope of a "using" clause, or you need to give the qualified name.
>
> Yeah, you're right. It would be tricky anyway, because it can't be
> sorted using normal name resolution rules, since it relies on type info
> which may not be dealt with until a subsequent stage.
>

C++ already has a remotely similar feature, when the type of the LHS of
the assignment/initialization affects the treatment of the RHS

// A set of overloaded functions
void foo();
void foo(int);
void foo(double);

int main()
{
void (*ptr)(double) = foo;
// Valid, automatically selects `void foo(double)`
}

In this example the process guided by the type on the LHS is not name
lookup, but overload resolution for the RHS. But the point is that the
general idea of passing type info from the LHS to the RHS is already
present in the language.

It can probably be done for enums as well, as in your example. But as it
has been noted above, the value of such feature would be rather small.

--
Best regards,
Andrey Tarasevich

Re: redeclaration of enumerators?

<463ad821-9983-4dae-b997-dba8e756c1fcn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18440&group=comp.lang.c#18440

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ad4:44f3:: with SMTP id p19mr442332qvt.33.1632947879676;
Wed, 29 Sep 2021 13:37:59 -0700 (PDT)
X-Received: by 2002:a37:4649:: with SMTP id t70mr1631593qka.493.1632947879402;
Wed, 29 Sep 2021 13:37:59 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Wed, 29 Sep 2021 13:37:59 -0700 (PDT)
In-Reply-To: <sj2adj$emu$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.249.78; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.249.78
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me> <sj1ro3$ng2$1@dont-email.me>
<sj1u8e$bds$1@dont-email.me> <sj1vgt$lfe$1@dont-email.me> <sj2adj$emu$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <463ad821-9983-4dae-b997-dba8e756c1fcn@googlegroups.com>
Subject: Re: redeclaration of enumerators?
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Wed, 29 Sep 2021 20:37:59 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 78
 by: Thiago Adams - Wed, 29 Sep 2021 20:37 UTC

On Wednesday, September 29, 2021 at 3:14:23 PM UTC-3, David Brown wrote:
> On 29/09/2021 17:08, Bart wrote:
> > On 29/09/2021 15:46, David Brown wrote:
> >> On 29/09/2021 16:03, Bart wrote:
> >
> >>> Yeah, you're right. It would be tricky anyway, because it can't be
> >>> sorted using normal name resolution rules, since it relies on type info
> >>> which may not be dealt with until a subsequent stage.
> >>>
> >>> But also, you could have this:
> >>>
> >>> enum E2 {A=1, B, C};
> >>> enum E2 A, B;
> >>>
> >>> A = E2::C
> >>> B = A;
> >>>
> >>> Is the RHS the variable A (which contains 3), or is it intended to be
> >>> E2::A, which is 1?
> >
> >> I checked on the "using enum" in C++. It was added in C++20, and lets
> >> you write:
> >>
> >> enum class colours { red, green, blue };
> >> colours my_favourite_colour;
> >>
> >> my_favourite_colour = colours::red;
> >> using enum colours;
> >> my_favourite_colour = blue;
> >
> > It took me a while to find something that supported C++20 (godbolt does)
> > to try it out.
> >
> > How it works is that if you use:
> >
> > using enum colours;
> >
> > then you can't declare:
> >
> > colours blue;
> >
> > So the ambiguity I pointed out can't occur.
> >
> > It stops you using 'int blue;' as well. In fact, all those enum names
> > become 'open', and you will get the same issues as you have in C with
> > clashes with other identifiers and other open enums.
> >
> > (Except that this feature allowes you to restrict those open enums to a
> > particular scope.)
> Of course. Typically, you'll have the "using enum" clause in quite a
> small scope - just the block of code where you need it. It's not
> something you'd have at file level.
> >
> > It doesn't help with my other example either: you can't use both:
> >
> > using enum colours;
> > using enum lights;
> >
> > as 'green' will clash; as well 'red', even though it they have the same
> > value.
> With strong enumerations, you shouldn't even be thinking of them as
> having an integer value - it doesn't make sense to compare the "value"
> of a "colours" enumeration element with one from a "lights" enumeration.
> That is a large part of the point of having better enumeration types
> than were available in C.

I don't mind too much about enumerators being integers into C standard.
But I would like compiler diagnostics (and I guess they already exist)
when enun types are mixed.

Attributes added a new punctuator in C23 that is '::' to be able to parse
scoped attributes like [[aa::bb]]

Maybe someday this punctuator will be used for something else in C,
to create scoped variables..

Re: redeclaration of enumerators?

<87tui2j1m3.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18441&group=comp.lang.c#18441

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Wed, 29 Sep 2021 18:25:40 -0700
Organization: None to speak of
Lines: 20
Message-ID: <87tui2j1m3.fsf@nosuchdomain.example.com>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me>
<sj1ro3$ng2$1@dont-email.me> <sj1u8e$bds$1@dont-email.me>
<sj1vgt$lfe$1@dont-email.me> <sj2adj$emu$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="4b075a85c80fa8b78011f466bfabf6da";
logging-data="15654"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ayfLJijtBAORy+PTfLmGB"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:2xXhHd/fdWYdmgDerysdYVe2Tb8=
sha1:2+u79OTsT1pjB9DY16CpXabLUjw=
 by: Keith Thompson - Thu, 30 Sep 2021 01:25 UTC

David Brown <david.brown@hesbynett.no> writes:
[...]
> With strong enumerations, you shouldn't even be thinking of them as
> having an integer value - it doesn't make sense to compare the "value"
> of a "colours" enumeration element with one from a "lights" enumeration.
> That is a large part of the point of having better enumeration types
> than were available in C.

C++ scoped enumeration types do have well-defined integer
representations. You can even specify them in the declaration:

enum class foo { zero = 0, one = 1 };

There's no implicit conversion from foo to int, but you can use a cast;
for example (int)foo::zero == 0.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: redeclaration of enumerators?

<88ffbaf7-99a4-4b7b-95d9-37625e1895b8n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18442&group=comp.lang.c#18442

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ad4:4651:: with SMTP id y17mr2699550qvv.62.1632989712024;
Thu, 30 Sep 2021 01:15:12 -0700 (PDT)
X-Received: by 2002:a05:622a:1652:: with SMTP id y18mr5078679qtj.226.1632989711845;
Thu, 30 Sep 2021 01:15:11 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 30 Sep 2021 01:15:11 -0700 (PDT)
In-Reply-To: <467a5838-5a51-4b4b-8e01-c28969b69a65n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23c7:a280:3401:a8aa:58d2:6ae1:81cd;
posting-account=3LA7mQoAAAByiBtHIUvpFq0_QEKnHGc9
NNTP-Posting-Host: 2a00:23c7:a280:3401:a8aa:58d2:6ae1:81cd
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>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <88ffbaf7-99a4-4b7b-95d9-37625e1895b8n@googlegroups.com>
Subject: Re: redeclaration of enumerators?
From: mark.blu...@gmail.com (Mark Bluemel)
Injection-Date: Thu, 30 Sep 2021 08:15:12 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 6
 by: Mark Bluemel - Thu, 30 Sep 2021 08:15 UTC

On Tuesday, 28 September 2021 at 23:23:40 UTC+1, Thiago Adams wrote:

> ... (This is allowed in defines for instance)
> #define A 1
> #define A 1

The macro preprocessing (cpp) which handles '#define" has little, if anything, to do with C syntax, and is basically a distraction here.

Re: redeclaration of enumerators?

<sj44gi$odk$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18443&group=comp.lang.c#18443

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Thu, 30 Sep 2021 12:45:38 +0200
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <sj44gi$odk$1@dont-email.me>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me>
<sj1ro3$ng2$1@dont-email.me> <sj1u8e$bds$1@dont-email.me>
<sj1vgt$lfe$1@dont-email.me> <sj2adj$emu$1@dont-email.me>
<87tui2j1m3.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 30 Sep 2021 10:45:38 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="d0db82f88345eb9a32c2288de1aca9c7";
logging-data="25012"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/chebzEWEYkS3TRX/hMosa3p829i+afvE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:/l+WoKsDr1l5ZEx/tbYgP5dWr/E=
In-Reply-To: <87tui2j1m3.fsf@nosuchdomain.example.com>
Content-Language: en-GB
 by: David Brown - Thu, 30 Sep 2021 10:45 UTC

On 30/09/2021 03:25, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
> [...]
>> With strong enumerations, you shouldn't even be thinking of them as
>> having an integer value - it doesn't make sense to compare the "value"
>> of a "colours" enumeration element with one from a "lights" enumeration.
>> That is a large part of the point of having better enumeration types
>> than were available in C.
>
> C++ scoped enumeration types do have well-defined integer
> representations. You can even specify them in the declaration:
>
> enum class foo { zero = 0, one = 1 };
>
> There's no implicit conversion from foo to int, but you can use a cast;
> for example (int)foo::zero == 0.
>

Sure - and for some use-cases that might be useful.

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.

Re: redeclaration of enumerators?

<bf3ac5ed-f44b-4f8d-9ffe-ad9583ade948n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18449&group=comp.lang.c#18449

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ad4:5541:: with SMTP id v1mr4182420qvy.49.1633012564355;
Thu, 30 Sep 2021 07:36:04 -0700 (PDT)
X-Received: by 2002:ae9:e90a:: with SMTP id x10mr5135453qkf.308.1633012564214;
Thu, 30 Sep 2021 07:36:04 -0700 (PDT)
Path: i2pn2.org!rocksolid2!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 30 Sep 2021 07:36:03 -0700 (PDT)
In-Reply-To: <sj44gi$odk$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:6066:f9ac:d0e8:ad75;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:6066:f9ac:d0e8:ad75
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me> <sj1ro3$ng2$1@dont-email.me>
<sj1u8e$bds$1@dont-email.me> <sj1vgt$lfe$1@dont-email.me> <sj2adj$emu$1@dont-email.me>
<87tui2j1m3.fsf@nosuchdomain.example.com> <sj44gi$odk$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <bf3ac5ed-f44b-4f8d-9ffe-ad9583ade948n@googlegroups.com>
Subject: Re: redeclaration of enumerators?
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Thu, 30 Sep 2021 14:36:04 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 14
 by: Malcolm McLean - Thu, 30 Sep 2021 14:36 UTC

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.

Re: redeclaration of enumerators?

<V5k5J.59463$6U3.29625@fx43.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18450&group=comp.lang.c#18450

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx43.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: redeclaration of enumerators?
Newsgroups: comp.lang.c
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com> <sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com> <sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me> <sj1ro3$ng2$1@dont-email.me> <sj1u8e$bds$1@dont-email.me> <sj1vgt$lfe$1@dont-email.me> <sj2adj$emu$1@dont-email.me> <87tui2j1m3.fsf@nosuchdomain.example.com> <sj44gi$odk$1@dont-email.me>
Lines: 28
Message-ID: <V5k5J.59463$6U3.29625@fx43.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Thu, 30 Sep 2021 14:47:17 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Thu, 30 Sep 2021 14:47:17 GMT
X-Received-Bytes: 2226
 by: Scott Lurndal - Thu, 30 Sep 2021 14:47 UTC

David Brown <david.brown@hesbynett.no> writes:
>On 30/09/2021 03:25, Keith Thompson wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>> [...]
>>> With strong enumerations, you shouldn't even be thinking of them as
>>> having an integer value - it doesn't make sense to compare the "value"
>>> of a "colours" enumeration element with one from a "lights" enumeration.
>>> That is a large part of the point of having better enumeration types
>>> than were available in C.
>>
>> C++ scoped enumeration types do have well-defined integer
>> representations. You can even specify them in the declaration:
>>
>> enum class foo { zero = 0, one = 1 };
>>
>> There's no implicit conversion from foo to int, but you can use a cast;
>> for example (int)foo::zero == 0.
>>
>
>Sure - and for some use-cases that might be useful.
>
>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.

But then you need something akin to the Pascal "PRED" and "SUCC" functions.

Re: redeclaration of enumerators?

<af0db3eb-7679-49ab-bc45-0675c1b5368fn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18453&group=comp.lang.c#18453

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:393:: with SMTP id l19mr6572376qvy.8.1633021851891;
Thu, 30 Sep 2021 10:10:51 -0700 (PDT)
X-Received: by 2002:ad4:4aa2:: with SMTP id i2mr5144432qvx.0.1633021851684;
Thu, 30 Sep 2021 10:10:51 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 30 Sep 2021 10:10:51 -0700 (PDT)
In-Reply-To: <V5k5J.59463$6U3.29625@fx43.iad>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:6066:f9ac:d0e8:ad75;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:6066:f9ac:d0e8:ad75
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me> <sj1ro3$ng2$1@dont-email.me>
<sj1u8e$bds$1@dont-email.me> <sj1vgt$lfe$1@dont-email.me> <sj2adj$emu$1@dont-email.me>
<87tui2j1m3.fsf@nosuchdomain.example.com> <sj44gi$odk$1@dont-email.me> <V5k5J.59463$6U3.29625@fx43.iad>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <af0db3eb-7679-49ab-bc45-0675c1b5368fn@googlegroups.com>
Subject: Re: redeclaration of enumerators?
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Thu, 30 Sep 2021 17:10:51 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 30
 by: Malcolm McLean - Thu, 30 Sep 2021 17:10 UTC

On Thursday, 30 September 2021 at 15:47:29 UTC+1, Scott Lurndal wrote:
> David Brown <david...@hesbynett.no> writes:
> >On 30/09/2021 03:25, Keith Thompson wrote:
> >> David Brown <david...@hesbynett.no> writes:
> >> [...]
> >>> With strong enumerations, you shouldn't even be thinking of them as
> >>> having an integer value - it doesn't make sense to compare the "value"
> >>> of a "colours" enumeration element with one from a "lights" enumeration.
> >>> That is a large part of the point of having better enumeration types
> >>> than were available in C.
> >>
> >> C++ scoped enumeration types do have well-defined integer
> >> representations. You can even specify them in the declaration:
> >>
> >> enum class foo { zero = 0, one = 1 };
> >>
> >> There's no implicit conversion from foo to int, but you can use a cast;
> >> for example (int)foo::zero == 0.
> >>
> >
> >Sure - and for some use-cases that might be useful.
> >
> >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.
> But then you need something akin to the Pascal "PRED" and "SUCC" functions.
>
Yes, that's another case I forgot.
You often want to iterate over all the values of an enum.

Re: redeclaration of enumerators?

<sj6csf$dvm$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18455&group=comp.lang.c#18455

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Fri, 1 Oct 2021 09:20:47 +0200
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <sj6csf$dvm$1@dont-email.me>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me>
<sj1ro3$ng2$1@dont-email.me> <sj1u8e$bds$1@dont-email.me>
<sj1vgt$lfe$1@dont-email.me> <sj2adj$emu$1@dont-email.me>
<87tui2j1m3.fsf@nosuchdomain.example.com> <sj44gi$odk$1@dont-email.me>
<bf3ac5ed-f44b-4f8d-9ffe-ad9583ade948n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 1 Oct 2021 07:20:47 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="b5f0ab6d56179f9e01a087c9f2a3444a";
logging-data="14326"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ZhPwcoPjFkI3iMRZN9mLTHcyuiJjb9dQ="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:2akmWDHWaRfbp72xAHBg44Jas3Q=
In-Reply-To: <bf3ac5ed-f44b-4f8d-9ffe-ad9583ade948n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Fri, 1 Oct 2021 07:20 UTC

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).

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.

Re: redeclaration of enumerators?

<sj6d3o$fej$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18456&group=comp.lang.c#18456

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Fri, 1 Oct 2021 09:24:39 +0200
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <sj6d3o$fej$1@dont-email.me>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me>
<sj1ro3$ng2$1@dont-email.me> <sj1u8e$bds$1@dont-email.me>
<sj1vgt$lfe$1@dont-email.me> <sj2adj$emu$1@dont-email.me>
<87tui2j1m3.fsf@nosuchdomain.example.com> <sj44gi$odk$1@dont-email.me>
<V5k5J.59463$6U3.29625@fx43.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 1 Oct 2021 07:24:40 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="b5f0ab6d56179f9e01a087c9f2a3444a";
logging-data="15827"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19SDlLsSERxZyVodTuG2mLpKG2Kpx9Ybyw="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:kifa0a4gOOWa1Wavp75mCDbi+74=
In-Reply-To: <V5k5J.59463$6U3.29625@fx43.iad>
Content-Language: en-GB
 by: David Brown - Fri, 1 Oct 2021 07:24 UTC

On 30/09/2021 16:47, Scott Lurndal wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 30/09/2021 03:25, Keith Thompson wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>> [...]
>>>> With strong enumerations, you shouldn't even be thinking of them as
>>>> having an integer value - it doesn't make sense to compare the "value"
>>>> of a "colours" enumeration element with one from a "lights" enumeration.
>>>> That is a large part of the point of having better enumeration types
>>>> than were available in C.
>>>
>>> C++ scoped enumeration types do have well-defined integer
>>> representations. You can even specify them in the declaration:
>>>
>>> enum class foo { zero = 0, one = 1 };
>>>
>>> There's no implicit conversion from foo to int, but you can use a cast;
>>> for example (int)foo::zero == 0.
>>>
>>
>> Sure - and for some use-cases that might be useful.
>>
>> 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.
>
> But then you need something akin to the Pascal "PRED" and "SUCC" functions.
>

Sometimes, yes, that would be nice. Often, however, your code is safer
if you /don't/ have such features. It might seem like a good idea to
have "go to next state" in your state machine, rather than giving the
next state explicitly, but you'll regret it when someone adds a new
state to the enumeration somewhere in the middle.

Re: redeclaration of enumerators?

<sj6nkg$4am$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18459&group=comp.lang.c#18459

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Fri, 1 Oct 2021 11:24:14 +0100
Organization: A noiseless patient Spider
Lines: 112
Message-ID: <sj6nkg$4am$1@dont-email.me>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me>
<sj1ro3$ng2$1@dont-email.me> <sj1u8e$bds$1@dont-email.me>
<sj1vgt$lfe$1@dont-email.me> <sj2adj$emu$1@dont-email.me>
<87tui2j1m3.fsf@nosuchdomain.example.com> <sj44gi$odk$1@dont-email.me>
<bf3ac5ed-f44b-4f8d-9ffe-ad9583ade948n@googlegroups.com>
<sj6csf$dvm$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 1 Oct 2021 10:24:17 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3c8cc88710cf1674d2bfd3ac7a214761";
logging-data="4438"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/gVXW3yx3pMODpZWK9hiVd0Vu8IzJsvkQ="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:Gm//rARN7bMLBKaOKkXKPfL0txE=
In-Reply-To: <sj6csf$dvm$1@dont-email.me>
X-Antivirus-Status: Clean
Content-Language: en-GB
X-Antivirus: AVG (VPS 211001-0, 1/10/2021), Outbound message
 by: Bart - Fri, 1 Oct 2021 10:24 UTC

On 01/10/2021 08:20, David Brown wrote:
> 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).
>
> 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 is not ideal:

* Each enum occurs three times (maybe it's to mimic loop variables!)

* There is no protection against defining [red] as "green"

* Entries can be in any order, so no protection against missing some out

* No dimension is given, so no protection against adding [amber] =
"amber" (unless amber's value clashes with red, green or blue

* If red, green and blue are given their own arbitrary values (so
non-consecutive), then this can generate, I assume, some arbitrary-sized
sparse array

* I can define [blue] twice with different values

* The whole thing consists of 3 components, with two visible global (eg.
in a header) and one needing to be kept apart.

I haven't really this either in my own language; I can never be bother
to do it properly. However I do use this feature:

tabledata() []ichar colour_names =
(red, $),
(green, $),
(blue, $),
end

The restriction is that values need to be consecutive, but this is
virtually always the case. (There are other features for named constants.)

* Each enum name occurs exactly once ($ can be replaced with a regular
string

* 'red' will always be "red" (unless overridden)

* It's impossible to miss out 'green' without removing it completely

* The dimension is always the number of enums; it's more obvious is
rogue values are added

* The ordering occurs in one place (swap red and blue, and it still
works, no other changes needed)

* You can't repeat the same enum more than once

* The whole thing is defined in one place. To share, mark it 'global'.

* Values must be consecutive (there are other features for regular named
constants)

* The restriction is that the start value, normally 1, can be overridden
using eg. red=0, but the array lwb ([0:] must match too, not checked.

* The big advantage it being able to add further arrays (extra columns)
with other related data with the same advantage of automatic correspondence.

> 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.

I fail to see that as an advantage. A compact, consecutive sequence
makes enums more suitable for:

* Switch-case values (allows a jump table to be used)

* Array indexing

* -- and ++ operations

* Allowing ordered comparisons, where ordering is important

Re: redeclaration of enumerators?

<e468277c-6174-4591-ac91-4ba7dcbf893cn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18460&group=comp.lang.c#18460

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:484:: with SMTP id 4mr9110925qkr.409.1633088946853;
Fri, 01 Oct 2021 04:49:06 -0700 (PDT)
X-Received: by 2002:ac8:7003:: with SMTP id x3mr12553002qtm.284.1633088946705;
Fri, 01 Oct 2021 04:49:06 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 1 Oct 2021 04:49:06 -0700 (PDT)
In-Reply-To: <sj6nkg$4am$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:74c6:f102:e11a:3268;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:74c6:f102:e11a:3268
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me> <sj1ro3$ng2$1@dont-email.me>
<sj1u8e$bds$1@dont-email.me> <sj1vgt$lfe$1@dont-email.me> <sj2adj$emu$1@dont-email.me>
<87tui2j1m3.fsf@nosuchdomain.example.com> <sj44gi$odk$1@dont-email.me>
<bf3ac5ed-f44b-4f8d-9ffe-ad9583ade948n@googlegroups.com> <sj6csf$dvm$1@dont-email.me>
<sj6nkg$4am$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <e468277c-6174-4591-ac91-4ba7dcbf893cn@googlegroups.com>
Subject: Re: redeclaration of enumerators?
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Fri, 01 Oct 2021 11:49:06 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 60
 by: Malcolm McLean - Fri, 1 Oct 2021 11:49 UTC

On Friday, 1 October 2021 at 11:24:29 UTC+1, Bart wrote:
> On 01/10/2021 08:20, David Brown wrote:
> > 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).
> >
> > 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 is not ideal:
>
For some purposes you really need a special function called something like "enum_to_string",
as David Brown implied. Then const char *name = enum_to_string(red), sets name to "red".
That would be good for debug messages,

printf("Car crashed lights were %s\n", enum_to_string( lightvalue));

maybe you could also use it in a database

struct crashrecord
{ double speed;
bool aidrivingon;
char lasttrafficlights[8]; // red, amber, green
};

However it wouldn't normally be acceptable for user-facing strings.

To implement, you would need enums to be types, then enum_to_string would be a bit like
"sizeof", a function (maths sense) which isn't a subroutine but a keyword.

Re: redeclaration of enumerators?

<sj6tkk$m4p$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18461&group=comp.lang.c#18461

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: redeclaration of enumerators?
Date: Fri, 1 Oct 2021 13:06:43 +0100
Organization: A noiseless patient Spider
Lines: 79
Message-ID: <sj6tkk$m4p$1@dont-email.me>
References: <a81bc0a9-beb2-4ec9-9c07-d1a3985b746an@googlegroups.com>
<sj010b$715$1@dont-email.me> <8735pojrc3.fsf@nosuchdomain.example.com>
<sj053r$6c4$1@dont-email.me> <sj1q5a$a7i$1@dont-email.me>
<sj1ro3$ng2$1@dont-email.me> <sj1u8e$bds$1@dont-email.me>
<sj1vgt$lfe$1@dont-email.me> <sj2adj$emu$1@dont-email.me>
<87tui2j1m3.fsf@nosuchdomain.example.com> <sj44gi$odk$1@dont-email.me>
<bf3ac5ed-f44b-4f8d-9ffe-ad9583ade948n@googlegroups.com>
<sj6csf$dvm$1@dont-email.me> <sj6nkg$4am$1@dont-email.me>
<e468277c-6174-4591-ac91-4ba7dcbf893cn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 1 Oct 2021 12:06:44 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3c8cc88710cf1674d2bfd3ac7a214761";
logging-data="22681"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19LHkeozCfjJTL/68Sc8rQ9Sq2DxV8mUo0="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:80ozzfcopfDvwJIuF6FQ7fLrpUE=
In-Reply-To: <e468277c-6174-4591-ac91-4ba7dcbf893cn@googlegroups.com>
X-Antivirus-Status: Clean
Content-Language: en-GB
X-Antivirus: AVG (VPS 211001-0, 1/10/2021), Outbound message
 by: Bart - Fri, 1 Oct 2021 12:06 UTC

On 01/10/2021 12:49, Malcolm McLean wrote:
> On Friday, 1 October 2021 at 11:24:29 UTC+1, Bart wrote:
>> On 01/10/2021 08:20, David Brown wrote:
>>> 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).
>>>
>>> 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 is not ideal:
>>
> For some purposes you really need a special function called something like "enum_to_string",
> as David Brown implied. Then const char *name = enum_to_string(red), sets name to "red".
> That would be good for debug messages,
>
> printf("Car crashed lights were %s\n", enum_to_string( lightvalue));
>
> maybe you could also use it in a database
>
> struct crashrecord
> {
> double speed;
> bool aidrivingon;
> char lasttrafficlights[8]; // red, amber, green
> };
>
> However it wouldn't normally be acceptable for user-facing strings.
>
> To implement, you would need enums to be types, then enum_to_string would be a bit like
> "sizeof", a function (maths sense) which isn't a subroutine but a keyword.
>

It would need a bit more than that. At the moment, C allows:

enum { red=1, green=1, blue=1 };

enum { red=1000000000, green=2000000000, blue=-2000000000 };

There are no restrictions at all. Think about how to_string might work
for any of these values. The first lot are impossible; the second,
inefficient.

Think also about using these as switch-case values, or array indices, as
I noted.

So the first step would be a special king of enum where the names are
inter-related and have a distinct, compact range of underlying values,
ideally consecutive.

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor