【发布时间】:2015-03-09 17:14:08
【问题描述】:
为什么这段代码不能编译,我该怎么做才能让它编译?
#include <iostream>
using namespace std;
enum class myEnum : unsigned int
{
bar = 3
};
int main() {
// your code goes here
unsigned int v = 2;
switch(v)
{
case 2:
break;
case myEnum::bar:
break;
}
return 0;
}
ideone:
prog.cpp: In function 'int main()':
prog.cpp:18:16: error: could not convert 'bar' from 'myEnum' to 'unsigned int'
case myEnum::bar:
在 GCC 和 Clang 中构建失败,在 MSVC 2013 中工作。
【问题讨论】:
-
static_cast<myEnum>(2) -
强类型枚举是强类型,并且没有隐式转换为整数类型。
-
我认为“:unsigned int”允许这样做?
-
不,这只是意味着枚举将存储在无符号整数中。枚举本身仍然是强类型的。
标签: c++ c++11 enum-class