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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, it’s not.
Actually, an
enum classis no class at all. Theclasskeyword is only used because suddenly changing the unscopedenumto a scopedenumwould 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 noenumcould have been namedclassin C++. They could have picked another, it would not have made much more sense anyway.However, despite the
classkeyword they are still regular enums in that only enumerators (and potentially values assigned to them) are allowed within the brackets.