A new C++0x feature called forward enum declarations allows you to declare an enumeration without providing its enumerators list. Learn how using it can avert long compilation times and ODR violations.

by Danny Kalev

Aug 13, 2009

Page 3 of 3

Forward Declarations of Enum Types

C++0x introduces a new syntactic construct—the forward declaration of an enum type—that allows you to declare an enumeration without providing its enumerators list. Such a declaration doesn't constitute a definition (in order to avoid problems with ODR). You can provide a forward declaration of an enum type only for enumerations with a fixed underlying type. You can redeclare an enumeration later (possibly providing the missing list of enumerators), but such a redeclaration must match the previous declaration. The following examples demonstrate these rules:

Because Airports is a scoped enum, its default enum base is int. When the compiler sees the following forward declaration, it knows what the underlying type of Airports is without looking at a list of enumerators:

enum class Airports;

In the above code listing, changing the list of Airports' enumerators doesn't require you to include the complete definition of Airports, so long as the enum base is the same. In this regard, enum forward declarations are similar to forward declarations of classes, structs, and unions.

Currently, a few compilers already support forward declarations of enum types, including C++ Builder 2009. Give this new C++0x feature a try and you may say goodbye to the long compilation times and ODR violations that C++03's enumerator list requirements can cause.