Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "enum class" a class type in C++?

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?

like image 794
msc Avatar asked Sep 25 '17 09:09

msc


People also ask

Are enums a type?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.

Is enum is a data type in C?

Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.

Does enum contain class?

C++11 has introduced enum classes (also called scoped enumerations), that makes enumerations both strongly typed and strongly scoped. Class enum doesn't allow implicit conversion to int, and also doesn't compare enumerators from different enumerations. To define enum class we use class keyword after enum keyword.

What is difference between enum and enum class?

An enum just spills its contents into the enclosing scope, and is basically a const static integer. This means that the first element of any default enum is the same using the == operator. Enum classes have their own scope, and don't pollute the namespace that they are in.


2 Answers

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.

like image 52
Vittorio Romeo Avatar answered Oct 06 '22 13:10

Vittorio Romeo


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.


1B. Stroustrup - C++11 FAQ

like image 36
StoryTeller - Unslander Monica Avatar answered Oct 06 '22 14:10

StoryTeller - Unslander Monica