Advertisement
Intermediate C++, Enums & Enum Classes

Can you forward declare an enum, and what are the rules?

Short answer

A scoped enum (or an unscoped enum with an explicit underlying type) can be forward declared because its size is known. A plain unscoped enum without a specified underlying type cannot be forward declared, since its size depends on its values.

Save C++ mock interview
Advertisement
AModel answer
A scoped enum (or an unscoped enum with an explicit underlying type) can be forward declared because its size is known. A plain unscoped enum without a specified underlying type cannot be forward declared, since its size depends on its values. Forward declaration reduces header dependencies.
enum class Color : int;   // forward declaration OK
enum Plain;               // error without underlying type
Real-world example
Forward declaring an enum class in a header avoids pulling in the full definition, cutting compile dependencies.
Likely follow-ups
  • Why can't a plain enum be forward declared? | What makes an enum forward-declarable?
Related topics