是“枚举类”吗? C ++中的类类型? [英] Is "enum class" a class type in C++?

查看:91
本文介绍了是“枚举类”吗? C ++中的类类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 cppreference 了解了C ++中的枚举声明。

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

然后我制作了 Enum类,并使用 std :: is_class

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-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.

没有提到枚举类 class 类型 在标准中的任何位置。

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

这篇关于是“枚举类”吗? C ++中的类类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆