GetDunne Wiki

Notes from the desk of Shane Dunne, software development consultant

User Tools

Site Tools


enum_class_rather_than_typedef_enum

This is an old revision of the document!


C++ enum declaration vs. C "typedef enum"

The notion of enumeration type was added to the C language in the ANSI C standard, first published in 1989, after the C language had already been in widespread use for a decade. The variable declaration

enum { kCat, kDog, kCow, kHorse } animal;

declares an integer variable animal, which is supposed to have only four permissible values, identified by the symbolic constant names kCat, kDog, kCow, and kHorse. It's common to see enum used together with typedef, to define an “enum type” (especially in a .h header file) which can subsequently be used to declare any number of variables with the same set of permissible values, e.g.

typedef enum { kCat, kDog, kCow, kHorse } Animal;
...
Animal a1, a2, a3;

This looks very nice, and it's tempting to think that we have succeeded in defining a truly new data-type, but in reality this is all just syntactic sugar. The C compiler treats enum variables just like integers (the number of bits used is compiler-specific). The user-specified symbolic names are assigned actual integer values by the compiler in an ordered sequence, usually starting with 0, and are also treated just like integers. That is, the declarations above are entirely equivalent to

#define kCat    0
#define kDog    1
#define kCow    2
#define kHorse  3
 
int animal;
int a1, a2, a3;

The following code would therefore be quite legitimate, and we quickly see that our hopes of defining a truly new data-type are nothing but empty hopes after all:

int x = kDog;        /* assign a symbolic Animal value to an int                   */
int y = kDog + 99;   /* use a symbolic Animal value in an int-valued expression    */
animal = 100;        /* assign an arbitrary int value to a variable of type Animal */

This is why enum constant-names are commonly prefixed with a lowercase “k” just like other #defined constants—because that's basically what they are.

In programming for embedded systems (especially microcontrollers), enum declarations are often used as a short-form alternative to groups of #defines, in cases where we would like to use symbolic names for values, but retain the power to specify their exact binary representation. This is entirely valid, even when carried over to C++.

The original C++ specification allowed (and encouraged) a slightly different enum declaration syntax:

enum Animal { cat, dog, cow, horse };

which is semantically equivalent to the C typedef declaration above. This is a bit unfortunate, because now we have two different syntaxes for the same type declaration, neither of which offers any type-safety. This has been rectified in the C++11 standard, with the introduction of enum classes.

For the kinds of C++ programs we write using JUCE, we will almost never need to specify the exact binary representation of symbolic values. C++ offers a subtly different way of using enum, which allows us to define true enumerated data types in a type-safe way. The C++ declaration

enum Animal { cat, dog, cow, horse };

defines a new type Animal, whose binary representation is arbitrary (we neither know, nor should we care, what it is), and a group of symbolic value-names which themselves are of type Animal.

bbbbbbbbbbbbbbb

In an early version of VanillaJuce, the file SynthEnvelopeGenerator.h included the declaration

typedef enum
{
    kIdle,
    kAttack,
    kDecay,
    kSustain,
    kRelease
} EG_Segment;
enum_class_rather_than_typedef_enum.1504226434.txt.gz · Last modified: 2017/09/01 00:40 by shane