Features


enum++ — An enum Class Code Generator

Arthur P. Walker

Neither C nor C++ have truly first-class enumerations. But you can add them to C++ with this handy generator.


The C++ enum expands upon the original C implementation by making each enum declaration a distinct type, and by disallowing casual conversions between enum and integral types. Despite these enhancements, however, the C++ enum still lacks several features that would greatly enhance its usefulness. These shortcomings include:

Implementing an enum-like class to correct these shortcomings will typically require that the code be modified in several places any time a change is made to the list of enum symbols. This problem multiplies as more and more enum-like classes are defined. To avoid these maintenance issues, I created enum_gen, an enum class code generator.

Generating an enum class using enum_gen is quite simple. First, create a text file containing the enum definition using standard C/C++ syntax. Next, invoke enum_gen with the enum definition file as an argument. That's it. As output, enum_gen creates a .h and a .cc file containing the enum class definition and implementation. The following snippet shows an example of an enum definition:

enum SwitchState
{
    OFF = 0,
    LOW = 33,
    MED = 66,
    HI = 100
};

Listing 1 shows the public interface of the class generated by enum_gen from this enum definition. (Space considerations prevent listing the full text of the enum_gen'd code. The full source code, plus the enum generator code is available on the CUJ ftp site. See p. 3 for downloading instructions.)

enum_gen follows the same rules used by C and C++ for assigning integer values to enum symbols that are not explicitly given a value. enum_gen deviates from C++ practice by requiring unique symbol values as well as unique symbol names. In addition to the symbols specified by the user-defined enum declaration, enum_gen adds the symbol _UNDEFINED to each enum class; _UNDEFINED is the default value of an uninitialized enum variable.

As might be expected, stream I/O with an enum_gen'd class is performed using the symbol strings. On input, if an unrecognized string is read into an enum class variable, the variable will be assigned a value of _UNDEFINED.

Enum class symbols are static members of the enum class, and thus must be specified using C++ scope resolution (e.g. MyEnum::ON).

Iteration through an enum symbol list is performed using operator++ and operator--. Enum symbol iteration is performed according to the order that the symbols are defined in the symbol list, not according to the sorted order of their integer values.

enum_gen'd classes more closely meet my needs for type-safe, bounded symbol sets than standard C++ enums. While enum classes aren't going to make or break your next project, they're nice to have in your toolbox when you need them. enum_gen makes implementation of enum classes trivial, and thus more likely to be useful in day-to-day development tasks.

Art Walker is a principal software engineer with the Essex Corp. of Columbia, MD. He can be reached at awalker@essexcorp.com.