Miscellaneous: Strongly typed enums

code

Syntax

Usage / Usability

Resolve the problems we had with the old enums

  • Can be silently converted to int
  • The enum ids are exported in the scope in which the enum is defined, causing name clashes
  • They have implementation-defined underlying type – type cannot be specified by the developer
  • Cannot use forward declaration

 

Problem: Can be silently converted to int

Solution

Problem : Name clashes

Solution : No more name clashes

Problem: Implementation defined type

  • They have implementation-defined underlying type – type cannot be specified by the developer

Solution: Type is specified by developer

Problem: Cannot use forward declaration

Solution: Can use forward declaration

PROS

  • Clear intent of what the developer wants
  • Avoid mistakes

Leave a comment

Your email address will not be published.