C++11 gave us enum class and while it’s great to have scoped enums I don’t find it great for error handling. Let’s talk about why.
C++ Enum Class and Error Codes
by Mathieu Ropert
From the article:
Most of my readers, I hope, have been able to use C++11 for a while now (if not hello, I’m sorry the world has changed to become this weird during your 14 years sleep). With it came a small change that allowed for better scoping of names without resorting to weird quirks:
enum class. The idea is simple: owing to C,enumvalues in C++ belong to the class or namespace they are declared in but if we add theclasskeyword to the declaration they know become their own scope instead of leaking to their parent.This was a simple quality of life change in the compiler to address a weakness in the language that folks usually worked around by either adding long prefix to the enum values (C style) or wrapping them within structs or classes (C++98/03 style). And with it came the incentive to migrate C era error code enums to scoped enums. But there’s a catch.

Add a Comment
Comments are closed.