You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What
Consider changing all enums to enum classes, this produces less surprises in code as things aren't implicitly converted to types
Why
Allows less surprises in code. See example below
enum Color { red, green, blue }; // plain enum enum Card { red_card, green_card, yellow_card }; // another plain enum enumclassAnimal { dog, deer, cat, bird, human }; // enum classenumclassMammal { kangaroo, deer, human }; // another enum classvoidfun() {
// examples of bad use of plain enums:
Color color = Color::red;
Card card = Card::green_card;
int num = color; // no problemif (color == Card::red_card) // no problem (bad)
cout << "bad" << endl;
if (card == Color::green) // no problem (bad)
cout << "bad" << endl;
// examples of good use of enum classes (safe)
Animal a = Animal::deer;
Mammal m = Mammal::deer;
int num2 = a; // errorif (m == a) // error (good)
cout << "bad" << endl;
if (a == Mammal::deer) // error (good)
cout << "bad" << endl;
}
That's interesting, never knew about it until now, thanks for pointing it out. I'll ensure enum class is used in the upcoming rewrite that is yet to be pushed upstream.
What
Consider changing all
enum
s toenum class
es, this produces less surprises in code as things aren't implicitly converted to typesWhy
Allows less surprises in code. See example below
Extra information: (optional)
https://stackoverflow.com/questions/18335861/why-is-enum-class-preferred-over-plain-enum
The text was updated successfully, but these errors were encountered: