1

What are the advantages/disadvantages of using namespace enums vs enum classes, e.g.

namespace Animals {
   enum { dog, cat, bird, fish };
}

vs.

enum class Animals { dog, cat, bird, fish };

My preference is to use enums in namespaces because then I don't have to deal with the annoying type conversion every time I want to print something, but I know people probably have good reasons for preferring enum classes, and I would like to learn why. For example, what kind of problems could I run into down the road with namespace enums, and are they considered bad practice?

Paradox
  • 1,935
  • 1
  • 18
  • 31
  • 1
    You already hit on it. What you call anoying `enum class` calls safe – NathanOliver Mar 30 '18 at 02:57
  • So the type conversion is the only practical difference between the two? Would you say namespace enums are still bad practice btw? – Paradox Mar 30 '18 at 02:58
  • 1
    Well, you can't really create a variable of your "namespace enum". It kind of limits its usefulness. I would do that if that was intended. Otherwise go with the `enum class`. – DeiDei Mar 30 '18 at 03:07
  • 1
    Oh I see, that's a good point. I guess I'd have to do: namespace Animals { enum AnimalTypes { dog, cat, bird, fish}; }, but then I'd have to go around defining new variables with: Animals::AnimalTypes newAnimal = Animals::dog; for example. – Paradox Mar 30 '18 at 03:12
  • REOPEN QUEUE: Note that the other question is about plain enums, not about namespace enums. – m7913d Mar 03 '22 at 20:32

0 Answers0