RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


C++0x Forward Enum Declarations Cut Down Compilation Time and Dependencies : Page 3

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.


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:

enum E: char;  // OK. forward declaration of an unscoped  
                //enum with char as the enum base
enum F; // Error. Enum base is required in fwd decl.
enum class G: short; //OK. Scoped, enum base is short
enum class H; //OK. Scoped, default enum base is int
enum E: char; //OK. Redeclaration of E
enum class E: char; //Error. Previously declared unscoped
enum E: int; //Error. Different enum base
enum class E: char 
{i,j,k,l}; // OK: this redeclaration is a definition
enum class E: char; //OK. Fwd decl.

Returning to the Airports example, you can now use the following forward declaration of Airports to compile the same code:

enum class Airports;

std::istream& operator>>(std::istream& is, Airports& ap);
std::string get_ap_name(Airports ap);

Airports ap;  
std::cin >> ap;
std::cout << get_ap_name(ap);

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.

Danny Kalev is a certified system analyst and software engineer specializing in C++. He was a member of the C++ standards committee between 1997 and 2000 and has since been involved informally in the C++0x standardization process. He is the author of "The ANSI/ISO Professional C++ Programmer's Handbook" and "The Informit C++ Reference Guide: Techniques, Insight, and Practical Advice on C++."
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date