Skip to content

Instantly share code, notes, and snippets.

@TheBB
Created December 13, 2021 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheBB/c643633150f5868a132c76b15c554de0 to your computer and use it in GitHub Desktop.
Save TheBB/c643633150f5868a132c76b15c554de0 to your computer and use it in GitHub Desktop.
Pybind11 repro
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
class MyClass {
private:
int value;
public:
MyClass() : value(0) {}
MyClass(int val) : value(val) {}
int getValue() const { return value; }
};
namespace pybind11 { namespace detail {
template <> struct type_caster<MyClass> : type_caster_base<MyClass> {
using base = type_caster_base<MyClass>;
public:
PYBIND11_TYPE_CASTER(MyClass, _("MyClass"));
bool load(handle src, bool convert) {
return base::load(src, convert);
}
static handle cast(MyClass src, return_value_policy policy, handle parent) {
return base::cast(src, policy, parent);
}
};
}}
PYBIND11_MODULE(pygold, m) {
py::class_<MyClass>(m, "MyClass")
.def(py::init<>())
.def(py::init<int>())
.def("__repr__",
[](const MyClass& obj) {
return "MyClass(" + std::to_string(obj.getValue()) + ")";
})
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment