简体   繁体   English

C / C ++创建一个带负值的枚举,无需编号

[英]C/C++ Create an enum with negative values, without having to number it

For example in C/C++, I would have the code: 例如在C / C ++中,我会得到代码:

typedef enum fruits{
   apple,
   banana,
   lemon,
   orange
} fruit_t;

Which would be equivalent to: 这相当于:

typedef enum fruits{
   apple = 0,
   banana = 1,
   lemon = 2,
   orange = 3
} fruit_t;

However, I would like the values to be negative, so they do not conflict with anything else. 但是,我希望这些值是负数,所以它们不会与其他任何东西发生冲突。 I could do it like this: 我可以这样做:

typedef enum fruits{
   apple = -1,
   banana = -2,
   lemon = -3,
   orange = -4
} fruit_t;

But if I would like to add another fruit, I have to assign another value, and if I put one inbetween, I have to renumber most of it. 但是,如果我想添加另一种水果,我必须分配另一个值,如果我在其中放置一个,我必须重新编号其中的大部分。 Is there an easier way of doing this? 有更简单的方法吗?

Start with INT_MIN on top like this: 首先从INT_MIN开始,如下所示:

#include <limits.h>

enum fruits
{
  orange = INT_MIN,
  lemon, 
  ...
}

Per " 5.2.4.2.1 Sizes of integer types " and/or " Annex E/1 " of the C11 Standard INT_MIN is at least -32767 . 根据C11标准INT_MININT_MIN 整数类型的大小 ”和/或“ 附件E / 1 ”至少为-32767

INT_MIN gets defined by including <limits.h> . INT_MIN通过包含<limits.h>定义。

In C you only need to number the top one and subsequent entries will be the previous entry +1 , this is covered in the C99 draft standard section 6.7.2.2 Enumeration specifiers which says ( emphasis mine ): C中,您只需要对前一个条目进行编号,后续条目将是前一个条目+1 ,这在C99草案标准部分6.7.2.2 枚举说明符中有所说明( 强调我的 ):

[...]An enumerator with = defines its enumeration constant as the value of the constant expression. [...]带有=的枚举数将其枚举常量定义为常量表达式的值。 If the first enumerator has no =, the value of its enumeration constant is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant.[...] 如果第一个枚举数没有=,则其枚举常量的值为0. 每个后续枚举数为no =将其枚举常量定义为通过将1加上前一个枚举常量值得到的常量表达式的值。[.. ]

and the wording is similar in the C++ draft standard section 7.2 Enumeration declarations paragraph 2 . C ++草案标准7.2节“ 枚举声明”2段中的措辞类似。

So just do something similar to the following: 所以只需做类似以下的事情:

#define MIN -4

typedef enum fruits{
   orange = MIN,
   lemon,  
   banana,
   apple,
} fruit_t;

Add news ones at the top. 在顶部添加新闻。 You still have to number the topmost one. 你仍然需要编号最顶层。

enum fruit
{
   orange = -4,
   lemon,
   banana,
   apple,
};

First of all, there is no such thing as "C/C++", those are two different languages. 首先,没有“C / C ++”这样的东西,它们是两种不同的语言。 You can't look at C as a subset of C++; 您不能将C视为C ++的子集; some legal C code won't compile as a C++ program. 某些合法的C代码不会编译为C ++程序。

If you can use C++11, I suggest you use enum class , which gives you strongly typed enumerations : 如果您可以使用C ++ 11,我建议您使用enum class ,它为您提供强类型枚举

enum class Traffic {red , yellow, green};
Traffic t = Traffic::red;

if ( t == Traffic::red )  // to test the value of an enum 

This way you can guarantee no clashing, because the compiler won't let you do any comparison with an integer or with a variable of a different enum type. 这样就可以保证不会发生冲突,因为编译器不会让你与整数或不同枚举类型的变量进行任何比较。

You can only compare against a variable of the same enum type, or use the binary scope resolution operator as in the example. 您只能与同一枚举类型的变量进行比较,或者使用示例中的二进制范围解析运算符。

Another advantage of this enum class is that you can set the size of your enum. 这个enum class另一个优点是你可以设置枚举的大小。 You can use any signed or unsigned integer type like this: 您可以使用任何有符号或无符号整数类型,如下所示:

enum class Traffic : char { red, yellow, green }

When a type is not specified, the default int is assumed. 如果未指定类型,则假定为默认int

Note that you need a compiler with C++11 support for this. 请注意,您需要一个支持C ++ 11的编译器。

I think you can order it anyway you like. 我想你无论如何都可以订购它。

If you are not giving any value then next enum will take +1 of its previous value. 如果您没有给出任何值,则下一个enum将获得其先前值的+1 If even fist enum is not having any value then it will start from 0 . 如果即使拳头enum没有任何值,那么它将从0开始。

But good practice is to have it in right order. 但好的做法是让它按正确的顺序排列。

It is also quite common to declare the errors as usual enumerations and use the negative value of it, like: 将错误声明为通常的枚举并使用它的负值也是很常见的,例如:

typedef enum fruits_e {
    APPLE=1,
    BANANA,
    LEMON,
    ORANGE
} fruit_t;

fruit_t eat_fastfood(void) {
    if (! me->healthy())
        return -APPLE;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM