【发布时间】:2015-08-27 15:07:08
【问题描述】:
enum class confirm {yes};
struct item
{
confirm s:4; // (1) limiting storage size required
};
int main()
{
item itm;
itm.s = confirm::yes; // (2) OK
switch (itm.s)
{
case confirm::yes: // (3) Failure, need static data cast here?
break;
}
}
产生错误:
In function ‘int main()’:
error: could not convert ‘yes’ from ‘confirm’ to ‘int’
case confirm::yes:
^
虽然用 g++ 编译,但用 clang++ 编译得很好。 为什么用 (2) 标记的赋值可能,但用 (3) 标记的 case 子句不可以?
关于too small storage 的警告是offtopic
【问题讨论】:
-
“这是一个 gcc 错误”会是一个足够的答案吗? (自然有证据表明它应该被允许)
-
@Yakk:有疑问
-
@dyp:感谢您的标记,但有一个问题是针对
enum class的——仅限 C++11;以前的标准没有这样的问题 -
我必须在 C++11 标签和其他一些标签之间进行选择(最多 5 个标签);我的理由是:[enum-class] 暗示 C++11,因此从标签中可以清楚地看出这是 >= C++11。三个 [switch-statement]、[bit-fields] 和 [enum-class] 描述了 [g++] 中的问题。我猜 [c++11] 标记无论如何都会在一段时间内变得多余,我看到各种问题已经接受了没有 [c++11] 标记的 C++11 答案。
-
标签: c++ g++ switch-statement bit-fields enum-class