They fixed Enumerations!

Well I just learned something awesome. Having started my C/C++ Journey with the C99 and C++99/gnu standard, I always avoided enumerations. But thanks to this one video I can finally hug them and love them and no more need for lookup tables. C++11/14/17 fixes so much of the scope poisoning that used to happen.

Thanks a million for this, I’m now going through the new C++11/14 standard doc again to reread other lovely treasures I forgot about/avoided in the past to see if I can finally use them.

To anyone curious: Standard C++


I seem to be out of likes for today, so I will comment.

I share your happiness about the scoping of enumeration-value names. I wish it were available in C Language also.

I am transposing an old-style C Language (and Literate Programming) game into modern open-source Clean C Language. There are some beautiful applications of enumerations there. There’s no trick for maintaining uniqueness (by prefixing or suffixing), although some enum definitions have CAPITALs to signify that they are like preprocess or constants.

I will probably leave them that way, since I am staying in C Language for now. It would be nice to do something to avoid future conflicts :).

Afterthought: I do think some sort of prefixing that makes it clear which enum a constant is part of will be useful in having the C code be more understandable. I don’t think it will have the strength of C++ enum class for strongly-typing parameters to functions though, since basically the C enum values are aliases for int values. Still, it might help in sight-checking. And maybe there is a trick for this … Still thinking it over … .

There are some tricks you could try.

I got around the scoping issue using #DEFINE [scope] and then enum [scope]_[enum name]

Overall for C Language I also completely avoided enum like sin.

Unless the source file is only for one small module(not a bad idea) you can also contain enum in a module aka linked library.

The problem I see is not about the name of the enum type in C Language but of the names of the enum items. In C and C++ (without enum class) It is as if each item in an enum definition is equivalent to a [global?] const int name = i;

I agree that one can use some form of qualifier on the names of the items so that there is likely no collision with any other constants. The advantage of enum is the avoidance of magic numbers and providing some level of semantic information about the intended significance.

It is also easy to update an enum definition and do other things with it. One can do cases on enum items but, unfortunately, not so easily define sets and name-value maps. Don Knuth and Don Woods came up with a clean way of doing that in the Literate Programming version of the game I am busy transposing to Clean C in open-source style. It is all done with run-time “loading” of hash tables and arrays, so you don’t have to depend on knowledge of the “index” of enumeration item values. (This is sort of constructors by-hand in the absence of classes.)

I think I can use Visual Studio refactoring to fix the names of items to incorporate qualification. I have to do that before separating the single file Knuth-Woods version into separate modules for using modern build solutions. If I refactor prematurely into separate files, I will have made a terrible mess for myself :wink:.

Thanks for your comment. You’ve helped sharpen my thinking on how to work this when working at the Clean C level.

Privacy & Terms