我正在阅读来自 here 的 Scoped enumerations
页面:
$ cat e.cxx
#include <cstdint>
enum class Handle : uint32_t { Invalid = 0 };
int main()
{
Handle h { 42 }; // OK
return 0;
}
$ g++ -std=c++11 e.cxx
e.cxx: In function ‘int main()’:
e.cxx:5:17: error: cannot convert ‘int’ to ‘Handle’ in initialization
Handle h { 42 }; // OK
^
使用:
$ g++ --version
g++ (Debian 5.3.1-14) 5.3.1 20160409
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
如果我现在检查 GCC 中的 C++11 支持,似乎自 GCC 4.8 以来一切都受支持。 .
那么我读错了哪一页? Score enumertions
的示例不是 100% 正确,或者 GCC 对 C++11 的支持仍然不完整?
最佳答案
enum class
(或 enum struct
)创建一个strong 类型。它不能简单地使用底层整数类型进行初始化,它需要显式转换,至少在 C++11 和 C++14 中是这样。
您显示的引用屏幕截图来自即将发布的 C++17 标准,该标准稍微放宽了要求,并允许这种类型的初始化。
关于c++ - 范围枚举 : error: cannot convert ‘int’ to ‘Handle’ in initialization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37413895/