I have a c++ class with an enum inside, and I wanted to mimick that with boost::python
, so that I can write MyClass.value
in python. boost::python::class_
does not have an enum_
method, and I was looking for workarounds.
I first tried with lambdas like
MyClass{ enum{value1,value2}; }; class_<MyClass>("MyClass").add_property("value1",&[](){return value1;}).staticmethod("value1");
which gives compiler error (in
get_signature
foradd_property
). I know I could create getter method for each of the values, but that seems very awkward to me (typing-wise).Using
attr
:auto classObj=class_<MyClass>("MyClass"); classObj.attr("value1")=(int)value1; classObj.attr("value2")=(int)value2;
but it cannot be chained like
.def
and other methods returning reference to the instance.
Is there a more elegant solution?