Pages

Enums are notoriously weakly typed in C. While pondering this the other day it occurred to me that instead of writing an enum like this:

enum suit { clubs, hearts, diamonds, spades };

you could write it like this:

struct suit
{
enum { clubs, hearts, diamonds, spades } value;
};

The struct wrapper adds a modicum of type safety. It also allows you to forward declare suit (you can't forward declare enums). The enumerators (clubs, hearts, diamonds, clubs) are still visible and can be used when a compile time value is required (e.g., switch cases). As usual, caveat emptor.