Problem
I have a very large std::vector
that gets returned from a C++ function, let's call it getVector()
.
Now I want to wrap that function in Cython:
cdef extern from "XY.h":
cdef cppclass _XY:
vector[double] getVector() except +
cdef class XY:
cdef _XY _this
...
def getVector():
return self._this.getVector()
As I want to avoid copying this large vector, I would like to make use of std::move. Like this:
cdef extern from "<utility>" namespace "std":
vector[double] move(vector[double]) # Cython has no function templates
This modifies the Cython source code in the following way:
def getVector():
return move(self._this.getVector())
Question
The above idea is not working. Cython is (at least) producing 1 copy of the vector. I assume this is because there is no way to move from a vector as this is already a Cython wrapper around the actual std::vector class from C++.
Is there another approach to avoid any copies? I would like to avoid returning a pointer from the C++ method.
There is probably a way by defining a C++ wrapper class that stores the vector and then move this class in Cython but I was wondering whether there is a way without (or very little) modifying the C++ source code.