简体   繁体   English

整数到枚举转换的static_cast

[英]static_cast on integer to enum conversion

There is some function that takes in an enum as argument 有一些函数将枚举作为参数

void myfunc(myEnum input);

As I understand, if I have to give an integer to this function, it is advised to explicitly cast it to enum, the reason being all integers may not be valid enum values. 据我了解,如果必须为此函数提供整数,建议将其显式转换为枚举,原因是所有整数可能都不是有效的枚举值。

As per MSDN 根据MSDN

"The static_cast operator can explicitly convert an integral value to an enumeration type. If the value of the integral type does not fall within the range of enumeration values, the resulting enumeration value is undefined." “ static_cast运算符可以将整数值显式转换为枚举类型。如果整数类型的值不在枚举值的范围内,则所得的枚举值不确定。”

and as per the C++ standards 5.2.9 Static cast -> 10 并按照C ++标准5.2.9静态转换-> 10

"A value of integral or enumeration type can be explicitly converted to an enumeration type. The value is unchanged if the original value is within the range of the enumeration values (7.2). Otherwise, the resulting value is unspecified (and might not be in that range)." “整数或枚举类型的值可以显式转换为枚举类型。如果原始值在枚举值(7.2)的范围内,则该值不变。否则,未指定结果值(并且可能不在该范围)。”

So what's the point using static_cast in this scenario? 那么在这种情况下使用static_cast什么意义呢? Is there some option that would raise exceptions on values outside the enum range (other than writing explicit code for that)? 是否有一些选项会引发枚举范围之外的值的异常(除了为此编写显式代码)?

As usual, the compiler is just trying to keep you from shooting yourself in the foot. 像往常一样,编译器只是在试图阻止您脚踏实地。 That's why you cannot just pass an int to a function expecting an enum. 这就是为什么您不能仅将int传递给需要枚举的函数。 The compiler will rightfully complain, because the int might not match any valid enum value. 编译器理应抱怨,因为int可能不匹配任何有效的enum值。

By adding the cast you basically tell the compiler 'Shut up, I know what I am doing' . 通过添加强制类型转换,您基本上可以告诉编译器“闭嘴,我知道我在做什么” What you are communicating here is that you are sure that the value you pass in is 'within the range of the enumeration values'. 您在此处传达的信息是,您确保传入的值“在枚举值的范围内”。 And you better make sure that is the case, or you are on a one-way trip to undefined-behavior-land. 而且,您最好确保是这种情况,否则您将单程前往未定义的行为领域。

If this is so dangerous, then why doesn't the compiler add a runtime check for the integer value? 如果这样做很危险,那么为什么编译器不为整数值添加运行时检查? The reason is, as so often with C++, performance. 原因是与C ++一样经常出现性能问题。 Maybe you just know from the surrounding program logic that the int value will always be valid and you absolutely cannot waste any time on stupid runtime checks. 也许你只是从周围的程序逻辑知道 int值将始终是有效的,你绝对不能浪费在无意义的运行时检查的任何时间。 From a language-design point of view, this might not be the most reasonable default to chose, especially when your goal is writing robust code. 从语言设计的角度来看,这可能不是最合理的默认选择,尤其是当您的目标是编写可靠的代码时。 But that's just how C++ works: A developer should never have to pay for functionality that they might not want to use. 但这就是C ++的工作方式:开发人员永远不必为自己可能不想使用的功能付费。

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

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