mardi 18 décembre 2018

read from pointer argument of a c++ function in python with pybind11

I have a c++ function like:

int add(int *i, int j) {
    *i += 3;
    return *i + j;
}

I have created python binding for it using pybind11 as

PYBIND11_MODULE(example, m) {
    m.doc() = R"pbdoc(add)pbdoc";
    m.def("add", &add, R"pbdoc(Add two numbers)pbdoc");
}

I call it in python as:

>>import example
>>a=1
>>example.add(a,2)
>>6 --> This is correct
>>a
>>1 --> This is not what expect

It returns 6, which is correct However, when I print "a", it still prints 1 instead of 4. How can i modify the pybind11 definition, so that changes done in argument value inside C++ as visible in python

Aucun commentaire:

Enregistrer un commentaire