Hacker News new | past | comments | ask | show | jobs | submit login

I broadly agree with the lots-of-pitfalls nature, but regarding the enum case specifically, which version of C++ are you talking about, and in what syntactic context? The later-introduced “enum class” makes things somewhat tighter than C-style enums.



All versions of C++, all existing versions and likely any potential future versions.

In C++ both these enum types have an "underlying type" which is just an integer. That's the type you're really getting for most purposes.

In terms of syntax specifically you can cast (in C++ specifically static cast) integers to an enumerated type. This makes mechanical sense to C++ because of the compatible "underlying type" but as a direct consequence you don't have the kind of type safety these exercises are relying on.

Notice that if I cast 1.234 to an integer, C++ says that's 1. It does know that cast doesn't mean "just change the type but keep the same bits" and yet, that's exactly what happens for enum.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: