2 ms·
It isn't type-safe in the sense you're used to in C++. But common pitfalls can be avoided if you don't destroy the type information by assigning through a vani
by loeg 10y ago
It isn't type-safe in the sense you're used to in C++. But common pitfalls can be avoided if you don't destroy the type information by assigning through a vanilla integer.
enum a { A };
enum b { B };
static enum a a = B;
$ clang -Wenum-conversion -Werror a.c
b.c:4:19: error: implicit conversion from enumeration type 'enum b' to different enumeration type 'enum a' [-Werror,-Wenum-conversion]
static enum a a = B;
~ ^
1 error generated.
- banachtarski 10y agoThe easy ability to assign it to a vanilla integer or cast it implicitly (through an arithmetic operator or comparison for example) is what I mean when I say they aren't typesafe. A warning is also a fair bit weaker than a flat out compile error, although I'll grant that compiling with -Werror (a really challenging thing for most companies) alleviates that.
- loeg 10y agoYeah, it isn't real type safety. That said, don't assign enums through vanilla integers if you don't have to. :-) Our engineering organization of 300+ people builds the majority of our code with -Werror (and a bunch of warnings enabled). Just an anecdote, of course.