为什么枚举类的默认类型与基本枚举类型不同? [英] why is the default type of enum class different than the underlying type of enum?

查看:60
本文介绍了为什么枚举类的默认类型与基本枚举类型不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问为什么以下代码在Visual Studio 2014 Update 4中产生错误.

I am asking why the following code yields an error in Visual Studio 2014 update 4.

enum A
{   a = 0xFFFFFFFF };

enum class B
{   b = 0xFFFFFFFF };

我知道我可以使用枚举类B:unsigned int .但是,为什么 enum 的默认基础类型与 enum class 的默认基础类型不同?应该有一个设计决定.

I know that I can use enum class B : unsigned int. But why is the default underlying type of enum different that the default underlying type of enum class? There should be a design decision.

说明我忘记提了这个错误:

Clarifications I forgot to mention the error:

错误C3434:枚举器值'4294967295'无法表示为'int',值是'-1'

error C3434: enumerator value '4294967295' cannot be represented as 'int', value is '-1'

这表明 enum类默认底层类型是 signed int ,而默认类型是 enum unsigned int .这个问题是关于标志部分的.

That suggests that the default underlying type of enum class is signed int while the default type of enum is unsigned int. This question is about the sign part.

推荐答案

就N4140而言,MSVC是正确的:

As far as N4140 is concerned, MSVC is correct:

§7.2/5每个枚举定义一个不同于所有类型的类型其他类型.每个枚举也都有一个基础类型.这可以使用 enum-base 明确指定基础类型.为了范围枚举类型,如果不是,则基础类型为 int 明确指定.[...]

§7.2/5 Each enumeration defines a type that is different from all other types. Each enumeration also has an underlying type. The underlying type can be explicitly specified using an enum-base. For a scoped enumeration type, the underlying type is int if it is not explicitly specified. [...]

从理论上讲,您可以阅读标题为强类型枚举(修订版3)N2347 .即, 2.2.2可预测/可指定类型(特别是签名)部分解释了 enum 的基础类型是实现定义的.例如,再次使用N4140:

For rationale, you can read the proposal entitled Strongly Typed Enums (Revision 3) N2347. Namely, section 2.2.2 Predictable/specifiable type (notably signedness) explains that the underlying type of enum is implementation-defined. For example, N4140 again:

§7.2/7对于基础类型不固定的枚举,基础类型是一个整数类型,可以表示所有枚举中定义的枚举值.如果没有整数类型可以代表所有枚举器值,则枚举格式不正确.它是实现定义的,哪种整数类型用作基础类型,但基础类型不得更大比 int 更好,除非枚举数的值不适合 int unsigned int .如果 enumerator-list 为空,则基础类型就像枚举有一个值为 0 的枚举.

§7.2/7 For an enumeration whose underlying type is not fixed, the underlying type is an integral type that can represent all the enumerator values defined in the enumeration. If no integral type can represent all the enumerator values, the enumeration is ill-formed. It is implementation-defined which integral type is used as the underlying type except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0.

以及N2347提出的解决方案:

And N2347's proposed solutions:

该提案分为两个部分,遵循迄今为止的EWG指示:

This proposal is in two parts, following the EWG direction to date:

•提供了一种独特的新枚举类型,该枚举类型具有以下所有功能:被认为是理想的:

• provide a distinct new enum type having all the features that are considered desirable:

o枚举器在其枚举的范围内

o enumerators are in the scope of their enum

o枚举器和枚举不会隐式转换为int

o enumerators and enums do not implicitly convert to int

o枚举具有定义的基础类型

o enums have a defined underlying type

•为纯枚举提供向后兼容的纯扩展名,并带有这些功能的子集

• provide pure backward-compatible extensions for plain enums with a subset of those features

o指定基础类型的能力

o the ability to specify the underlying type

o使用枚举名称限定枚举数的能力

o the ability to qualify an enumerator with the name of the enum

针对不同的新枚举类型建议的语法和措辞为基于此功能的C ++/CLI [C ++/CLI]语法.建议现有枚举扩展的语法旨在实现相似性.

The proposed syntax and wording for the distinct new enum type is based on the C++/CLI [C++/CLI] syntax for this feature. The proposed syntax for extensions to existing enums is designed for similarity.

因此,他们采用了解决方案,为范围内的枚举提供了定义的基础类型.

So they went with the solution to give scoped enums a defined underlying type.

这篇关于为什么枚举类的默认类型与基本枚举类型不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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