Skip to content

How to pass c++ objects as a parameter by reference to python functions? #3932

Answered by Holt59
taguhiM asked this question in Q&A
Discussion options

You must be logged in to vote

You need to 1) import the module containing the bindings for std::vector<double> and 2) wrap the value in std::ref (or pass a pointer):

#include <memory>
#include <iostream>
#include <vector>
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>

namespace py = pybind11;

PYBIND11_MAKE_OPAQUE(std::vector<double>);

PYBIND11_EMBEDDED_MODULE(module, m)
{
    py::bind_vector<std::vector<double>>(m, "VectorDouble");
}

int main() {
    std::vector<double> V{1, 2, 3};
    py::scoped_interpreter guard{};
    py::module_ cpp_module = py::module_::import("module");
    py::module_ py_function = py::module_::import("py_function");
    auto

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by taguhiM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants