Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a (deep) copy constructor for pybind11::array_t?

I have an existing pybind11::array_t, and need to do a copy-construction. Is there a function inside pybind11 that allows me to do a deep copy of an array_t?

I know that I could create a new array_t, size it properly, and then copy the original data into it, but was wondering if there exists already a method to do this that hides these passages.

like image 962
fnrizzi Avatar asked Nov 07 '22 16:11

fnrizzi


1 Answers

the default copy constructor does a deep copy, people are actually trying to avoid this :)

To use the copy constructor, you can go through the buffer

using py_arr = pybind11::array_t<double>;
py_arr a;

// do stuff with a, fill it and everything...

auto buffer = a.request(); 
py_arr b = py_arr(buffer);

std::cout << b.data() << " " << a.data() << std::endl; // this won't return the same address twice
like image 173
Christian Avatar answered Nov 15 '22 10:11

Christian