小编koz*_*zmo的帖子

在switch语句中从int到enum类的隐式转换

enum class pid
{
    Alpha, Beta, Gamma
};

int main()
{
    int propId = 2;
    switch(propId)
    {
    case pid::Alpha:
    case pid::Beta:
    case pid::Gamma:
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的片段在msvc2012中编译好(并且有效),但在clang-3.4和g ++ - 4.8中失败.这些需要static_cast<pid>(propId)在switch子句中使用.

顺便说一句,没有显式转换的简单赋值,例如pid a = propId;在每个编译器中给出错误.

哪一个做对了?

c++ type-conversion c++11 enum-class

4
推荐指数
1
解决办法
7785
查看次数

标签 统计

c++ ×1

c++11 ×1

enum-class ×1

type-conversion ×1