在使用强类型枚举(strongly typed enums)进行 switch 语句时,是否有方法可以避免显式转换为 int
?
/// @desc an enumeration of the states that the session can be in.
enum class State
{
Created,
Connected,
Active,
Closed
};
State sesState = session->GetState();
switch (static_cast<int>(sesState))
{
case static_cast<int>(Session::State::Created):
// do stuff.
break;
case static_cast<int>(Session::State::Connected):
// do stuff.
break;
}
根据n3242草案:
6.4.2 Switch语句 [stmt.switch]
2 条件必须是整型、枚举类型或者一个类类型,该类型必须存在单个非显式转换函数将其转换为整型或枚举类型 (12.3)。
枚举类型是否包括强类型的枚举,或者它们与switch
语句不兼容因为需要显式转换为int
?