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.
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 typeReal-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