3 ms·
I believe: enum Foo { FOO_A, FOO_B }; is implicitly convertible to int even in C++. enum class Foo { FOO_A, FOO_B }; is not implicitly convertible, but
by goodcanadian 6y ago
I believe:
enum Foo { FOO_A, FOO_B };
is implicitly convertible to int even in C++.
enum class Foo { FOO_A, FOO_B };
is not implicitly convertible, but can still be converted using static_cast.
- deleted 6y ago[deleted]
- quietbritishjim 6y agoIndeed, but I'm talking about the other way: int -> Foo. The article talked about "different rules for implicit conversion" so I assume they must have been referring to that direction.
- qalmakka 6y agoThis. Only `enum class` gives you a strongly-typed guarantee, `enum` can happily implicitly decay to `int` without any kind of warning. Lots of C libraries use `int` frequently together with enums without any sort of care, and it's too late to change this behaviour without creating an interoperability nightmare. The fact that `int` is not implicitly convertible to an enum type at least still saves you from a death from thousand cuts when a wrong value manages to get into an enum. The only sane way is to consider `enum` as a deprecated construct in C++ and use it as sparingly as possible, preferably only when interoperability with C makes them unavoidable.
- quietbritishjim 6y agoThis article is about code that will directly be compiled by both a C and C++ compiler, so indeed `enum class` is not an option here.