Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

c++ - Interfacing C++11 array with Cython

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 :

cdef extern from "<array>" namespace "std" nogil :
    cdef cppclass array[T, size_t]:
        ctypedef T value_type
        cppclass iterator:
            T& operator*()
            iterator operator++()
            iterator operator--()
            iterator operator+(size_t)
            iterator operator-(size_t)
        bint operator==(iterator)
        bint operator!=(iterator)
        bint operator<(iterator)
        bint operator>(iterator)
        bint operator<=(iterator)
        bint operator>=(iterator)
        T& operator[](size_t)
        array() except +
        array(array&) except +

Most of this file is an adaptation from "vector.pxd", but I removed the allocator because c++11 array doesn't need it. I used size_t as second template argument but I am not sure about it.

The problem is, when I do :

    from array11 cimport array
    cdef array[int, 5] test   

I get, when compiling :

unknown type in template argument

If I do :

    from array11 cimport array
    cdef array[int, 5] * test = new array[int, 5]()

I get :

new operator can only be applied to a C++ class

Any idea about what I am doing wrong?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...