[英]pybind11: Enum vs Enum Class?
I understand the difference between enums and enum classes in the context of C++, but in the context of binding enums and enum classes is there any real difference?我理解枚举和枚举类在 C++ 上下文中的区别,但是在绑定枚举和枚举类的上下文中,有什么真正的区别吗?
Say for example:比如说:
enum class options {
maybe,
yes,
no,
};
enum words {
hello,
world
};
I've just been binding them the same way (see below), so my question is, am I overlooking something, or are enums and enum classes effectively identical in the context of binding them to python with pybind11?我刚刚以相同的方式绑定它们(见下文),所以我的问题是,我是否忽略了某些东西,或者在使用 pybind11 将它们绑定到 python 的上下文中,枚举和枚举类是否有效相同?
py::enum_<options>(m, "options")
.value("maybe", options::maybe)
.value("yes", options::yes)
.value("no", options::no);
py::enum_<words>(m, "words")
.value("hello", words::hello)
.value("world", words::world)
According to the docs , it appears that the only difference is:根据docs ,似乎唯一的区别是:
The
enum_::export_values()
function exports the enum entries into the parent scope, which should be skipped for newer C++11-style strongly typed enums.enum_::export_values()
函数将枚举条目导出到父作用域,对于较新的 C++11 风格的强类型枚举,应该跳过它。
So by not calling export_values
, Python will require the enum name as part of the scope when specifying values, which is conceptually closer to enum class
than enum
.因此,通过不调用
export_values
,Python 在指定值时将需要 enum 名称作为范围的一部分,这在概念上更接近enum class
不是enum
。
Worth noting that Python's Enum
doesn't allow implicit int
conversions like C++'s enum
.值得注意的是,Python 的
Enum
不允许像 C++ 的enum
那样进行隐式int
转换。 For that you need IntEnum
- but I'm not sure if you can create IntEnum
s from pybind.为此你需要
IntEnum
-但我不知道你是否可以创建IntEnum
从pybind秒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.