42

I read about enumeration declaration in C++ using cppreference.

Then I have made Enum class and check whether it is a class type or not using std::is_class.

#include <iostream>

enum class Enum 
{
    red = 1, blue, green
};

int main() 
{
    std::cout << std::boolalpha;
    std::cout << std::is_class<Enum>::value << '\n';
}

Then I compiled and ran in G++ compiler on Linux platform, it prints false value.

So Is enum class type or not? If enum is a class type, then why I'm getting false value?

2

2 Answers 2

47

enum class is not a class definition - the combination of keywords is used to define a scoped enumeration, which is a completely separate entity from a class.

std::is_class correctly returns false here. If you use std::is_enum, it will return true.


From the Standard:

The enumeration type declared with an enum-key of only enum is an unscoped enumeration, and its enumerators are unscoped enumerators. The enum-keys enum class and enum struct are semantically equivalent; an enumeration type declared with one of these is a scoped enumeration, and its enumerators are scoped enumerators.

There is no mention of an enum class being a "class type" anywhere in the Standard.

Sign up to request clarification or add additional context in comments.

4 Comments

"There is no mention of an enum class being a class type anywhere." except for the keyword used to declare it in the code :/
@will: edited my answer, hopefully clarifying what I meant
it wasn't a jibe at your answer, but more at the syntax chosen for enums...
Is it syntax or semantics? (I don't mean that in the colloquial sense, as in 'pedantics'.) I'm looking at another question regarding typename and class being synonymous within the context of template parameters, e.g. enum class EC {}; template<class C> TC { typedef std::is_class<C> type; }; (TC<EC> is well-formed, but its type::value is false.) It would seem that 'class' as a keyword (sometimes? only?) qualifies the type/context you're referring to. I'd love to hear it articulated better - the refs only bother describing individual cases.
23

Despite the class keyword, enumerations are not classes. That keyword only means the enumerators must respect certain scoping rules (and also prevents implicit integral conversions).

The choice of the keyword is due to the aspects brought about by the new type1, and how scoped enumerators were hacked together in the pre-C++11 era, to obtain said aspects:

struct Enum { // could just as well be a class.
  enum {
    red = 1, blue, green
  };
};

Which only allowed the enumerators to be accessed via the qualified name. Though it didn't prevent implicit conversions like true scoped enumerations do.

is_class is meant to identify the class/struct aggregate types.


1 B. Stroustrup - C++11 FAQ

4 Comments

Stroustrup says this regarding choice of the keyword: "The new enums are "enum class" because they combine aspects of traditional enumerations (names values) with aspects of classes (scoped members and absence of conversions)." (from C++11 FAQ).
@zett42 - Shamelessly added to the answer. Thank you!
I suspect that one of the primary reasons it is called "enum class" rather than something more explicit (such as "scoped enum") is simply for keyword re-use. Adding new keywords to the language is a difficult process as it can break backwards compatibility, which is why "final", for example, is not a keyword but an identifier. "enum class" tenuously made sense and re-used existing keywords, so they went with that. Pure conjecture, of course :)
@AzCopey - I'd put money on your wager :) I suspect the fact it also happens to have similarities to an existing idiom is a happy coincidence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.