Sujet : Re: C23 thoughts and opinions
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 23. May 2024, 21:23:15
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240523095110.449@kylheku.com>
References : 1 2 3 4 5
User-Agent : slrn/pre1.0.4-9 (Linux)
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.
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).
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.)
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca