当前位置:  开发笔记 > 编程语言 > 正文

C++枚举是签名还是未签名?

如何解决《C++枚举是签名还是未签名?》经验,为你挑选了6个好方法。

C++枚举是签名还是未签名?通过扩展,通过检查输入<=您的最大值来验证输入是安全的,并且省去> =您的最小值(假设您从0开始并递增1)?



1> Michael Burr..:

我们来看看源头.这是C++ 03标准(ISO/IEC 14882:2003)文档在7.2-5(枚举声明)中所说的内容:

枚举的基础类型是一个整数类型,可以表示枚举中定义的所有枚举器值.它是实现定义的,其中整数类型用作枚举的基础类型,除了基础类型不应大于int,除非枚举器的值不能适合int或unsigned int.

简而言之,您的编译器可以选择(显然,如果您的某些枚举值有负数,它将被签名).



2> zvrba..:

您不应该依赖任何特定的表示.阅读以下链接.此外,标准表示它是实现定义的,哪个整数类型用作枚举的基础类型,除了它不应大于int,除非某些值不能适合int或unsigned int.

简而言之:你不能依赖于签名或未签名的枚举.


[Michael Burr的答案](http://stackoverflow.com/a/159308/594137)(引用标准)实际上暗示你*可以*依赖它被签名如果你将枚举值定义为负值,因为类型是能够"表示枚举中定义的所有枚举器值".

3> Adam Rosenfi..:

您不应该依赖它们签名或未签名.如果要使它们明确签名或未签名,可以使用以下命令:

enum X : signed int { ... };    // signed enum
enum Y : unsigned int { ... };  // unsigned enum


仅在未来的C++ 0x标准中.
@dalle Microsoft compilator也允许键入的枚举http://msdn.microsoft.com/en-us/library/2dzy4k6e(v=vs.80).aspx

4> Matt..:

您不应该依赖签名或未签名.根据标准,它是实现定义的,其中整数类型用作枚举的基础类型.但是,在大多数实现中,它是有符号整数.

在C++ 0x中,将添加强类型枚举,这将允许您指定枚举的类型,例如:

enum X : signed int { ... };    // signed enum
enum Y : unsigned int { ... };  // unsigned enum

但是,即使是现在,也可以通过将枚举用作变量或参数类型来实现一些简单的验证:

enum Fruit { Apple, Banana };

enum Fruit fruitVariable = Banana;  // Okay, Banana is a member of the Fruit enum
fruitVariable = 1;  // Error, 1 is not a member of enum Fruit
                    // even though it has the same value as banana.



5> Cristián Rom..:

编译器可以决定枚举是已签名还是未签名.

验证枚举的另一种方法是使用枚举本身作为变量类型.例如:

enum Fruit
{
    Apple = 0,
    Banana,
    Pineapple,
    Orange,
    Kumquat
};

enum Fruit fruitVariable = Banana;  // Okay, Banana is a member of the Fruit enum
fruitVariable = 1;  // Error, 1 is not a member of enum Fruit even though it has the same value as banana.



6> JavaMan..:

即使是一些旧的答案也有44个赞成票,我倾向于不同意所有这些答案.简而言之,我认为我们不应该关心underlying type枚举.

首先,C++ 03 Enum类型是一个独特的类型,没有符号概念.从C++ 03标准开始dcl.enum

7.2 Enumeration declarations 
5 Each enumeration defines a type that is different from all other types....

因此,当我们讨论枚举类型的符号时,比如在使用<运算符比较2个枚举操作数时,我们实际上是在讨论将枚举类型隐式转换为某种整数类型.重要的是这种整体类型的标志.当将枚举转换为整数类型时,此语句适用:

9 The value of an enumerator or an object of an enumeration type is converted to an integer by integral promotion (4.5).

而且,显然,枚举的基本类型与积分促销无关.由于标准定义了Integral Promotion,如下所示:

4.5 Integral promotions conv.prom
.. An rvalue of an enumeration type (7.2) can be converted to an rvalue of the first of the following types that can represent all the values of the enumeration
(i.e. the values in the range bmin to bmax as described in 7.2: int, unsigned int, long, or unsigned long.

因此,枚举类型是否成为signed intunsigned int取决于是否signed int可以包含定义的枚举数的所有值,而不是枚举的基础类型.

请参阅我的相关问题 转换为积分类型后C++枚举类型的符号不正确

推荐阅读
虎仔球妈_459
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有