[C++] Proper enum usage

Started by
19 comments, last by Antheus 14 years, 2 months ago
Quote:Original post by Vanderry
I must say that I'm a bit surprised so many of you oppose the use of enums when it seems so widely used, not the least in many professional DirectX-applications I have studied. I guess that might be one of those fields where their pros outweigh their cons.


I'm only opposed towards treating enums and ints as equivalent, or using them as typesafe replacement for numeric constants.

At some point, they need to be written somewhere, and then there's that old mapping problem again, and the first thing will be assuming literal int interpretation. But then this is written externally, code changes, and suddenly old values no longer make sense, yet there is no check in place, or even possible.

And not just C or C++. For example, before 1.5/5, Java lacked enum type, so a cookie-cutter version was patternized. Except that the implementation (static/singletonish-based one even) that was de-facto standard had some obscure serialization/initialization issues which would result in same enum labels having different values, sometimes even inside same VM when serialization was involved.

IMHO - either full static type safety, or full dynamic language. The things in between tend to annoying in the long term, even if over short-term a tool, perhaps an inference type analyzer can help a bit.

This is especially true for C++ under modern compilers which are good at eliminating unused code, or minimizing memory layout of such convenience wrappers.

While likely overbloated and over-engineered, perhaps look at boost::error's design.

This topic is closed to new replies.

Advertisement