[英]Cython C++ templates
I am fairly new to cython, and I was attempting to wrap a templated vector class defined as 我是cython的新手,我试图包装一个定义为的模板化矢量类
template < typename T, uint N >
struct Vector{}
and I am having a difficult time learning about how cython uses templates, especially those with an int as an argument. 我很难了解cython如何使用模板,特别是那些以int作为参数的模板。 I read in the docs that ints are not yet supported as template parameters. 我在文档中读到还没有支持int作为模板参数。 How do I do this properly? 我该怎么做呢?
I found an easy solution! 我找到了简单的解决方案!
In a C++ header file, you can just declare a typedef, for example 例如,在C ++头文件中,您可以声明一个typedef
typedef Vector<float,3>; Vector3f;
In your cython file you can just declare that and now you can use all the functions and operators within the class. 在您的cython文件中,您可以声明它,现在您可以使用该类中的所有函数和运算符。
cdef extern from "Vector.h" namespace "ns":
cdef cppclass Vector3f:
Now, I had an additional issue, and that is with "specialized" functions, in my case a specialization for a Vector with 3 params. 现在,我有一个额外的问题,那就是“专业”功能,在我的例子中是一个带有3个参数的Vector的专门化。
template<typename T1, typename T2>
inline Vector<T1, 3 >Cross(const Vector <T1, 3 > & v1, const Vector<T2, 3> & v2)
To use this in cython, just declare it outside the class, in my case 要在cython中使用它,在我的情况下,只需在类外声明它
cdef extern from "Vector.h" namespace "ns":
cdef cppclass Vector3f:
...
Vector3f Cross(Vector3f v1,Vector3f v2)
For the curious, the Cython wiki shows how to write a templated class in Cython: 对于好奇的, Cython wiki展示了如何在Cython中编写模板化的类:
cdef extern from "<vector>" namespace "std":
cdef cppclass vector[T]:
...
Furthermore, multiple template parameters are defined as a list. 此外,多个模板参数被定义为列表。 To answer the OP's question, one would use cdef struct Vector[T, N]
. 要回答OP的问题,可以使用cdef struct Vector[T, N]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.