简体   繁体   English

将枚举与整数进行比较

[英]Comparing enums to integers

I've read that you shouldn't trust on the underlying implementation of an enum on being either signed or unsigned.我读过你不应该相信枚举的底层实现是签名还是未签名。 From this I have concluded that you should always cast the enum value to the type that it's being compared against.由此我得出结论,您应该始终将枚举值转换为与之比较的类型。 Like this:像这样:

enum MyEnum { MY_ENUM_VALUE = 0 };

int i = 1;
if (i > static_cast<int>(MY_ENUM_VALUE))
{
    // do stuff
}

unsigned int u = 2;
if (u > static_cast<unsigned int>(MY_ENUM_VALUE))
{
    // do more stuff
}

Is this the best practice?这是最佳做法吗?

Edit: Does the situation change if the enum is anonymous?编辑:如果枚举是匿名的,情况会改变吗?

An enum is an integer so you can compare it against any other integer, and even floats. enum是一个整数,因此您可以将其与任何其他整数进行比较,甚至是浮点数。 The compiler will automatically convert both integers to the largest, or the enum to a double before the compare.在比较之前,编译器会自动将两个整数转换为最大值,或者将枚举转换为双精度数。

Now, if your enumeration is not supposed to represent a number per se, you may want to consider creating a class instead:现在,如果您的枚举本身不应该代表一个数字,您可能需要考虑创建一个类:

enum class some_name { MY_ENUM_VALUE, ... };

int i;
if(i == static_cast<int>(some_name::MY_ENUM_VALUE))
{
    ...
}

In that case you need a cast because an enum class is not viewed as an integer by default.在这种情况下,您需要进行强制转换,因为默认情况下枚举类不被视为整数。 This helps quite a bit to avoid bugs in case you were to misuse an enum value...这有助于避免错误,以防您误用枚举值......


Update: also, you can now specify the type of integer of an enum .更新:另外,您现在可以指定enum的整数类型。 This was available in older compilers too, but it was often not working quite right (in my own experience).这在较旧的编译器中也可用,但它通常不能正常工作(根据我自己的经验)。

enum class some_name : uint8_t { ... };

That means the enumeration uses uint8_t to store those values.这意味着枚举使用uint8_t来存储这些值。 Practical if you are using enumeration values in a structure used to send data over a network or save in a binary file where you need to know the exact size of the data.如果您在用于通过网络发送数据或保存在需要知道数据确切大小的二进制文件中的结构中使用枚举值,则非常实用。

When not specified, the type defaults to int .未指定时,类型默认为int


As brought up by others, if the point of using enum is just to declare number, then using constexpr is probably better.正如其他人所提出的,如果使用enum的目的只是为了声明数字,那么使用constexpr可能会更好。

constexpr int MY_CONSTANT_VALUE = 0;

This has the same effect, only the type of MY_CONSTANT_VALUE is now an int .这具有相同的效果,只是MY_CONSTANT_VALUE的类型现在是int You could go a little further and use typedef as in:您可以更进一步并使用typedef ,如下所示:

typedef int my_type_t;
constexpr my_type_t MY_CONSTANT_VALUE = 0;

I often use enum even if I'm to use a single value when the value is not generally considered an integer.我经常使用enum ,即使我要使用单个值,而该值通常不被视为整数。 There is no set in stone rule in this case.在这种情况下,没有一成不变的规则。

Short answer: Yes简短的回答:是的

enum is signed int type, but they get implicitly cast into unsigned int . enum是有signed int类型,但它们被隐式转换为unsigned int Your compiler might give a warning without explicit casting, but its still very commonly used.您的编译器可能会在没有显式转换的情况下发出警告,但它仍然非常常用。 however you should explicitly cast to make it clear to maintainers.但是,您应该明确地转换以使维护者清楚。

And of course, explicit cast will be must when its a strongly typed enum .当然, 当它是强类型 enum时,显式转换是必须的

Best practice is not to write最佳实践是不写

int i = 1;
if (i > static_cast<int>(MY_ENUM_VALUE))
{
    // do stuff
}

instead write而是写

MyEnumValue i = MY_ENUM_VALUE ;
...
if ( i > MY_ENUM_VALUE ) {..}

But if - as in your example - you only have one value in your enum it is better to declare it as a constant instead of an enum.但是如果 - 就像在你的例子中一样 - 你的枚举中只有一个值,最好将它声明为常量而不是枚举。

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

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