Deutsch   English   Français   Italiano  
<v2q80g$2c2bt$2@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: C23 thoughts and opinions
Date: Fri, 24 May 2024 16:25:20 +0200
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <v2q80g$2c2bt$2@dont-email.me>
References: <v2l828$18v7f$1@dont-email.me>
 <00297443-2fee-48d4-81a0-9ff6ae6481e4@gmail.com>
 <v2lji1$1bbcp$1@dont-email.me> <20240523154331.000063da@yahoo.com>
 <v2ngf7$1p3o2$2@dont-email.me> <20240523095110.449@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 24 May 2024 16:25:20 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="aa31083d92649562fe347cd52a2a5ec5";
	logging-data="2492797"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/o5GjB6DIO1TGsMcRWlZDM8WWzBC3SeSw="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
 Thunderbird/102.11.0
Cancel-Lock: sha1:FhSIXRqYqiURrNPEIpYS65V5N/Y=
In-Reply-To: <20240523095110.449@kylheku.com>
Content-Language: en-GB
Bytes: 2750

On 23/05/2024 22:23, Kaz Kylheku wrote:
> On 2024-05-23, David Brown <david.brown@hesbynett.no> wrote:
>> So yes, I /could/ use enum constants for things that are not
>> enumerations.  I /did/ use them for that.  But going forward with C23,
>> I'll use constexpr instead.
> 
> The value of an enum is:
> 
> 1. Compiler warns of incomplete switch cases.

(gcc -Wswitch or -Wswitch-enum)

To be clear - I will, without doubt, continue to use "enum" for 
enumerations and enumerated types.  For enumerations, enum gives all the 
advantages you mention and more (such as automatic choice of values).

But I'd rather use "constexpr" for constant expressions that are not 
enumerations.

> 
> 2. In a debugger when you examine an enum-valued expression or
>     variable, you get the symbolic name:
> 
> 3. Safety (with C++ enum rules: no implicit
>     conversion from ordinary integer type to enum).

(gcc -Wenum-compare -Wenum-conversion -Wenum-int-mismatch)

> 
> Historically, C code bases have abused enums to defined constants
> like "enum { bufsize = 1024 }" for understandable reasons, but it is a
> cringe-inducing hack, which is also incomplete and inflexible; e.g. what
> if we want a floating-point constant.
> 
> I've benefited from (3) in C programs that were contrived
> to be compilable as C++. (That practice, though, tends to increasingly
> hamper your dialect choice though, as the languages diverge and make
> only small steps here and there to become closer.)
>