c++ - 模板类中的 Cython C++ 静态方法

标签 c++ templates cython

问题

我有一个 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/

相关文章:

c++ - 使用二进制 write() read() 将 vector <char> 作为流连接

c++ - 使用 glReadPixels 时是否可以不限制数据?

django模板ModelChoiceField长度

c++ - 如何删除析构函数中的void指针?

python - Cython 无法正常工作,无法诊断问题

c++ - 从 'int' 到 'char *' 的无效转换

c# - SendMessage 中的误导参数类型 (... IntPtr wParam ...)

c++ - 具有不完全整数类型的结构和枚举

cython - 使用扩展类型(cdef)和设置纯Python代码有什么区别?

python - 使用 Cython 的 bool numpy 数组