Ayaz Salikhov Ayaz Salikhov - 3 months ago 48
C++ Question

Wrapping method returning c++ std::array<std::string, 4> in cython

My method returns

std::array<std::string, 4>
in C++ code. I wrap this code using Cython. I tried to wrap array using memory views. But the result is
Invalid base type for memoryview slice: string
. So can I wrap my
std::array<std::string, 4>
to use it in python like list of strs?

Answer

The easiest way is probably just to copy to a Python list.

For the sake of this answer I'm assuming you've wrapped your array similar to this answer and called it arrstr4. The code then looks something like:

def f():
    cdef arrstr4 res = your_cplus_plus_function()
    py_res = []
    for i in range(4):
        py_res.append(res[i]) # take advantage of autoconversion to python string
    return py_res