我习惯于构建 C++ 程序并在 Cython 中获取它,但在这里我试图获取 C++ 11array
这绝对行不通。
这是我的 .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 +
该文件的大部分内容是对“vector.pxd”的改编,但我删除了分配器,因为 c++11array
不需要它。我用了size_t
作为第二个模板参数,但我不确定。
问题是,当我这样做时:
from array11 cimport array
cdef array[int, 5] test
编译时我得到:
模板参数中的未知类型
如果我做 :
from array11 cimport array
cdef array[int, 5] * test = new array[int, 5]()
I get :
new 运算符只能应用于 C++ 类
知道我做错了什么吗?
Thanks!
正如评论中所讨论的,您遇到的问题是因为 Cython 并不真正支持非类型模板参数。一种解决方法(hacky 并且可能很脆弱)是欺骗 Cython 认为它提供了类型模板参数:
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]
诀窍是如果你这样做cdef cppclass name "somestring"
Cython 只会盲目替代somestring
名称,它生成正确的 C++ 代码。这种方法显然有一些限制,但对于简单的使用来说应该没问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)