Normally you can define a cast for a class by using the following syntax:
class Test {
public:
explicit operator bool() { return false; }
};
Is there a way to do this or something similar for an enum class
?
c++c++11enum-classenums
Normally you can define a cast for a class by using the following syntax:
class Test {
public:
explicit operator bool() { return false; }
};
Is there a way to do this or something similar for an enum class
?
Best Answer
No, it's not.
Actually, an
enum class
is no class at all. Theclass
keyword is only used because suddenly changing the unscopedenum
to a scopedenum
would have mean reworking all enums codes. So the committee decided that to distinguish between new-style and old-style enums, the new ones would be tagged withclass
, because it's a keyword already so noenum
could have been namedclass
in C++. They could have picked another, it would not have made much more sense anyway.However, despite the
class
keyword they are still regular enums in that only enumerators (and potentially values assigned to them) are allowed within the brackets.