4

I'm trying to cast a custom type according to the pybind11 docs. But I don't know how to cast a struct with multiple field. what I'm trying to do so far is below

    struct vect {
        vect(int x, int y): x(x), y(y){}
        int x, y;
    };

    int main() {
        py::scoped_interpreter guard{};
        py::module my_mod = py::module::import("my_mod");
        vect v(1,2);
        py::object result = my_mod.attr("do_something")(v);
        vect v2 = result.cast<vect>();
        return 0;
    }

someone help me to cast it

namespace pybind11 {
    namespace detail {
        template <> struct type_caster<vect> {
            // how to cast c++ --> python and pyton --> c++
        };
    }
} // namespace pybind11::detail
some dev
  • 99
  • 1
  • 6

0 Answers0