C/C++ 创建一个带有负值的枚举,而不必给它编号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21027298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C/C++ Create an enum with negative values, without having to number it
提问by Joe
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?
但是如果我想添加另一个水果,我必须分配另一个值,如果我在中间放置一个,我必须重新编号大部分。有没有更简单的方法来做到这一点?
回答by alk
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 标准的“ 5.2.4.2.1 整数类型的大小”和/或“附件 E/1”,INT_MIN
至少是-32767
.
INT_MIN
gets defined by including <limits.h>
.
INT_MIN
通过包含来定义<limits.h>
。
回答by Shafik Yaghmour
In Cyou only need to number the top one and subsequent entries will be the previous entry +1
, this is covered in the C99 draft standardsection 6.7.2.2
Enumeration specifierswhich says (emphasis mine):
在C 中,您只需要对顶部的条目进行编号,随后的条目将是前一个条目+1
,这在C99 草案标准部分6.7.2.2
Enumeration specifiers 中进行了说明(强调我的):
[...]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 1to the value of the previous enumeration constant.[...]
[...]带有 = 的枚举器将其枚举常量定义为常量表达式的值。如果第一个枚举器没有 =,则其枚举常量的值为 0。每个后续没有 = 的枚举器将其枚举常量定义为前一个枚举常量的值加 1 得到的常量表达式的值。[.. .]
and the wording is similar in the C++ draft standardsection 7.2
Enumeration declarationsparagraph 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;
回答by Chad
Add news ones at the top. You still have to number the topmost one.
在顶部添加新闻。你仍然需要给最上面的一个编号。
enum fruit
{
orange = -4,
lemon,
banana,
apple,
};
回答by Moha the almighty camel
First of all, there is no such thing as "C/C++", those are two different languages. You can't look at C as a subset of C++; some legal C code won't compile as a C++ program.
首先,没有“C/C++”这样的东西,那是两种不同的语言。您不能将 C 视为 C++ 的子集;一些合法的 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. You can use any signed or unsigned integer type like this:
这样做的另一个优点enum class
是您可以设置枚举的大小。您可以使用任何有符号或无符号整数类型,如下所示:
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 的编译器。
回答by Digital_Reality
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. If even fist enum
is not having any value then it will start from 0
.
如果您没有给出任何值,则 nextenum
将采用+1
其先前的值。如果连拳头enum
都没有任何价值,那么它将从0
.
But good practice is to have it in right order.
但好的做法是按正确的顺序排列。
回答by Florin Hillebrand
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;
}