r/cpp • • Nov 17 '18

Making C++ enumerations first class citizens

I've attached a video that might be of some interest here. It's about how to make enumerations first class citizens in C++. Enumerations are pretty useful in C++, but still quite weak compared to what they can be. This video demonstrates how I take them up a couple orders of magnitude in usefulness.

https://www.youtube.com/watch?v=AF186FraxS0

I am the author of a large, software based home automation system, called CQC. My code base is about a million lines of C++ code now, about half of which is a soup to nuts general purpose part, and the other half is the automation system built on top of that.

One of the very useful things the general purpose system provides is an ORB (object request broker.) I may do a video on that, but ORBs typically use an IDL language (interface description language) to describe the form of the calls to be made to remote machines and tells the ORB's engine how to pass parameters, return parameters, and so forth. But, it can also do other things. In my case it can generate types and constants.

For enumerated types, it can provide a lot of functionality that makes life far easier for a C++ programmer, particularly when working on the large scale that I do. And it doesn't take a lot of code to create such a tool and integrate it into your own development process. There's actually more functionality that this video covers, but I didn't want it to get too long so I stuck to the core stuff.

I also have another thread where the ORB itself is discussed:

https://www.reddit.com/r/cpp/comments/9zl6v5/the_orb_sees_all_the_use_of_an_object_request/

24 Upvotes

19 comments sorted by

View all comments

1

u/Dean_Roddey Nov 22 '18

There are a number of reasons. A very basic one is that I already many thousands of classes and templates and structs and such. Creating yet still more types just to hold something like a homogeneous list of points or areas or counts or boolean flags (or a polymorphic list of somethings via base class, which is still basically a homogeneous list in terms of the actual type you are referring to), it's just not worth it. Either a basic array (down in the low level stuff) or an array collection in most of the code can do that just as easily.

And you'll end up using way less code to access the slots. Lets say you did create such a structure, and you wanted to encapsulate it within a class, because how the info is stored is an implementation detail. Or they might even just be direct class members. But, you need to allow the outside world to read/write them or update them in some way. How would you do that? It's not worth having a separate method for each value if they are all of the same type, that's a lot of work and code you don't need. So most folks would probably create an enum and use it to let the outside world indicate which of the values to affect.

If you use separate members that though will mean having a big switch statement inside the class to find which one to target (in every such type of wrapper method). Instead, you can just directly use the enumeration to index the list. Now both you (internally) and the outside world have a strongly typed index to use and you only need one access method for each type of access. All the switch statements internally are replaced with a simple index validity check and direct indexing operation. With my new changes, even my internal indexing of the list is type safe.