pybind11: Enum vs Enum Class?

Viewed 1361

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?

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?

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)
1 Answers

According to the docs, it appears that the only difference is:

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.

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.

Worth noting that Python's Enum doesn't allow implicit int conversions like C++'s enum. For that you need IntEnum - but I'm not sure if you can create IntEnums from pybind.

Related