Interfacing C++11 array with Cython

前端 未结 1 1804
走了就别回头了
走了就别回头了 2021-01-21 03:19

I am used to build C++ programs and get it in Cython, but here I am trying to get the C++ 11 array and it definitely doesn\'t work.

Here is my .pxd :

<
相关标签:
1条回答
  • 2021-01-21 03:48

    As discussed in the comments, the issue you're having is because Cython doesn't really support non-type template arguments. A workround (hacky and probably fragile) is to trick Cython into thinking it's providing a type template argument:

    cdef extern from "<array>" namespace "std" nogil :
        cdef cppclass two "2":
            pass
    
        cdef cppclass array[T, U]:
            T& operator[](size_t)
            # this is obviously very very cut down
    
    def f():
        cdef array[int,two] x
        return x[0]+x[1]
    

    The trick is that if you do cdef cppclass name "somestring" Cython will just blindly replace somestring for name, which generates the correct C++ code. There are obviously some limitations with this approach but for simple usage it should be fine.

    0 讨论(0)
提交回复
热议问题