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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With