Is “enum class” a class type in C++?

前端 未结 2 898
予麋鹿
予麋鹿 2021-02-05 00:15

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:

2条回答
  •  半阙折子戏
    2021-02-05 01:12

    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

提交回复
热议问题