Created
December 13, 2021 10:45
-
-
Save TheBB/c643633150f5868a132c76b15c554de0 to your computer and use it in GitHub Desktop.
Pybind11 repro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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