是“枚举类”吗? C ++中的类类型? [英] Is "enum class" a class type in C++?
问题描述
我使用 cppreference 了解了C ++中的枚举声明。
I read about enumeration declaration in C++ using cppreference.
然后我制作了 Enum类,并使用 std :: is_class $ c $检查它是否是一个类类型。 c>。
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';
}
然后我在 G ++ 编译器上编译并运行在Linux平台上,它显示 false
值。
Then I compiled and ran in G++ compiler on Linux platform, it prints false
value.
所以是枚举
是否是类类型?如果枚举是类类型,那么为什么我会得到 false 值?
So Is enum
class type or not? If enum is a class type, then why I'm getting false value?
推荐答案
枚举类
不是类
的定义-关键字的组合用于定义作用域枚举,它是与类
完全独立的实体。
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
在此处正确返回 false
。如果使用 std :: is_enum
,它将返回 true
。
std::is_class
correctly returns false
here. If you use std::is_enum
, it will return true
.
从标准:
使用仅
enum
的枚举键声明的枚举类型是一个无范围的枚举,它的枚举数是无范围的枚举数。枚举键enum class
和enum struct
在语义上是等效的;用其中之一声明的枚举类型是作用域枚举,其枚举器是作用域枚举器。
The enumeration type declared with an enum-key of only
enum
is an unscoped enumeration, and its enumerators are unscoped enumerators. The enum-keysenum class
andenum struct
are semantically equivalent; an enumeration type declared with one of these is a scoped enumeration, and its enumerators are scoped enumerators.
没有提到枚举类
是 class
类型 在标准中的任何位置。
There is no mention of an enum class
being a "class
type" anywhere in the Standard.
这篇关于是“枚举类”吗? C ++中的类类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!