Liste des Groupes | Revenir à cl c |
On 2024-05-23, David Brown <david.brown@hesbynett.no> wrote:(gcc -Wswitch or -Wswitch-enum)So yes, I /could/ use enum constants for things that are notThe value of an enum is:
enumerations. I /did/ use them for that. But going forward with C23,
I'll use constexpr instead.
1. Compiler warns of incomplete switch cases.
2. In a debugger when you examine an enum-valued expression or(gcc -Wenum-compare -Wenum-conversion -Wenum-int-mismatch)
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.)
Les messages affichés proviennent d'usenet.