问题
我有一个 C++ 模板类,它有一个静态方法。它看起来或多或少像这样:
template<typename T>
class Foo {
static std::shared_ptr<Foo<T>> doSth();
}
所以在 C++ 中你可以这样调用它:Foo<Int>::doSth();
.然而在 Cython 中,调用静态方法的方式是使用类名作为命名空间:
cdef extern from "Bar.h" namespace "Bar":
shared_ptr[Bar] doSth() # assuming shared_ptr is already declared
但这没有模板的概念。显然,简单地传递 Foo<T>
作为命名空间不起作用,因为它转换为 Foo<T>::doStr()
在 C++ 中,没有具体类型替代 T。
问题
你会如何在 Cython 中做到这一点?有办法或解决方法吗?
最佳答案
Cython 现在直接支持静态方法;不再需要或推荐命名空间 hack。
cdef extern from "Foo.h":
cdef cppclass Foo[T]:
@staticmethod
shared_ptr[Foo[T]] doSth() # assuming shared_ptr is already declared
cdef shared_ptr[Foo[int]] shared_ptr_value = Foo[int].doSth()
http://docs.cython.org/en/latest/src/userguide/wrapping_CPlusPlus.html#static-member-method
关于c++ - 模板类中的 Cython C++ 静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31119814/