Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

c++11 - 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 << '
';
}

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?

question from:https://stackoverflow.com/questions/46401502/is-enum-class-a-class-type-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...