Pybind11: binding anonymous enum

Viewed 192

I have the following struct:

struct DataStruct {
  enum { ID = importantData };
  int data1;
  int data2;
}

How would I bind the anonymous enum? I have my current binding below, but so far I'm just skipping the enum.

py::class_<DataStruct>(m, "DataStruct")
  .def(py::init<>())
  .def_readwrite("data1", &DataStruct::data1)
  .def_readwrite("data2", &DataStruct::data2);

Note that this struct will be used as an input to a function also bound to python via pybind11, so the goal is more so to be able to create the object in Python then pass it to the C++ function bound within python, than it is to strictly read the contents of the struct.

1 Answers

Per the docs, define it as such:

py::class_<DataStruct> data_struct(m, "DataStruct");

data_struct.def(py::init<>())
    .def_readwrite("data1", &DataStruct::data1)
    .def_readwrite("data2", &DataStruct::data2);

py::enum_<DataStruct::DataStructEnum>(data_struct, "DataStructEnum")
    .value("ID", DataStruct::DataStructEnum::ID)
    .export_values();
Related