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

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.


very declaration of an enum type must be a complete definition containing a full list of its enumerators. However, in some use cases declaring an enumeration without providing the enumerators list is desirable. For example, you may use a forward declaration like this...

enum class Direction;

...instead of repeating the complete definition (see below) in every translation unit.

enum class Direction: short {Up, Down};

A year ago, the C++ standards committee voted into the Working Draft a proposal to add forward declarations of enum types. This 10-Minute Solution shows how to use forward declarations of enum types to expedite compilation time and reduce code dependencies.

Your project has an enum type containing hundreds of machine-generated enumerators that change frequently. You must provide a complete definition of that enum in every translation unit, even if the compiler seemingly doesn't need the complete definition of the enum.

Replace complete definitions of enum types with forward enum declarations where possible.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date