Why are enums so tedious?

It’s long, long past time for C/C++ to have some automated way to reflect enums and/or build enums from strings.

enum class ProductType : unsigned char {  Cheese, Whine, Max } ;

const char[ProductType::Max] ProductTypeName = {  "Whine", "Cheese" } ;

struct ProductTypeInfo { ProductType type, const char* name, size_t minOrder, size_t maxOrder, Branch factory } ;
ProductTypeInfo productTypeInfo[ProductType::Max] =
  { Whine, "Whine", 1, 10, Branch::Bordeaux }
, { Cheese, "Cheese", 1, 50, Branch::Cheshire }
switch ( productType )
case ProductType::Cheese: testForCheddar() ; break ;
case ProductType::Whine: hiccup() ; break ;
/* Compiler warning: No test for 'Max' */

ARRRRGGHHH! SO MUCH DUPLICATION OF DATA! (And I hope you spotted the [most egregious] mistake).

Firstly, it’s time for enums to become printable.

enum class ProductType { Whine, Cheese, Max } ;
 void nameThatType(ProductType t)
 // It doesn't have to be *this* easy, but ...
 printf("That's a %u ['%s' or '%E']\n",  t, t) ;
 nameThatType(ProductType::Whine) ;
 // products That's a 0 ['Whine' or  'ProductType::Whine']

Secondly, there needs to be a way to get the upper and lower bounds of an enum range so that we don’t have damn “Max” type names everywhere that we have to try and remember how people named.

enum Types { BadData = -1, Uninitialized = 0, Froglok, Goat, Elf, Dwarf, Ninja, Max } ;

// becomes

enum Types { Uninitialized = 0, Froglok, Goat, Elf, Dwarf, Pirate } ;

template<class EnumType>
bool enumValidate(EnumType e)
return e >= EnumType::lowest() && e <= EnumType::highest() ;

And lastly, there needs to be a way to generate them by association.

// One option, a special case, allow char arrays to construct enums.
// But frankly, if enums convert to strings, it's not required,

const char* productTypes[ : enum class ProductType : unsigned char] =
"Whine" // Auto-generates ProductType::Whine
, "Cheese" // Auto-generates ProductType::Cheese

// Another option, allow enums to say "build me from strings",
// so that you can incorporate string arrays from elsewhere:
#if defined __cplusplus2100__
enum class ProductType from
const char* productTypes[] =
"Whine", "Cheese"

// But this might perhaps work.
struct ProductInfo { enum ProductIndex productIndex using name, char name[], size_t minOrder, size_t maxOrder } ;
ProductInfo pi[] =
{ "Cheese", 1, 10  }  // Because "productIndex" was specified, it is automatically injected with the enum value
, { "Whine", 1, 50 }
// Equivalent to
// , { ProductType::Whine, "Whine", 1, 50 }
// Each row also has a member, "productIndex" which is automatically set to the equivalent enum,

struct Products { enum ProductId using name, char code[4], char name[], Department dept, float cost, float price } ;
// This version doesn't create a "ProductId" member variable, because I didn't give it a name.

Leave a Reply

Name and email address are required. Your email address will not be published.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

You may use these HTML tags and attributes:

<a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <pre> <q cite=""> <s> <strike> <strong> 

%d bloggers like this: