模板类中的Cython C ++静态方法 [英] Cython C++ static methods in a template class
问题描述
我在C ++中有一个带有静态方法的模板类。看起来或多或少是这样的:
I have a template class in C++ that has a static method. It looks more or less like this:
template<typename T>
class Foo {
static std::shared_ptr<Foo<T>> doSth();
}
因此在C ++中,您会这样称呼: Foo< ; Int> :: doSth();
。但是,在Cython中,调用静态方法的方法是使用类名作为命名空间:
so in C++ you would call it like: Foo<Int>::doSth();
. In Cython however, the way to call static methods is by using the classname as a namespace:
cdef extern from "Bar.h" namespace "Bar":
shared_ptr[Bar] doSth() # assuming shared_ptr is already declared
但这没有模板的概念。显然,仅将 Foo< T>
作为命名空间传递是行不通的,因为它会转换为 Foo< T> :: doStr()
but this has no notion of templates. Obviously, simply passing Foo<T>
as a namespace doesn't work, because it translates to Foo<T>::doStr()
in C++, no concrete type is substituted for T.
你会怎么做?在Cython中这样做吗?有办法还是解决方法?
How would you do that in Cython? Is there a way, or a workaround?
推荐答案
注意:当时的答案是正确的它已经写过(并且仍然可以使用),但是您现在应该使用 @Robertwb对此问题的答案来正确执行此操作。
Note: This answer was right at the time it was written (and does still work) but you should now use @Robertwb's answer to this question instead to do this properly.
我认为您不能直接在Cython中进行操作。您可以为普通的(非静态方法)c ++模板函数创建非常薄的包装器
I don't think you can do it directly in Cython. You could create very thin wrapper of a normal (non-static-method) c++ template function
template <typename T>
std::shared_ptr<Foo<T>> Foo_T_doSth<T>() {
return Foo<T>::doSth();
}
,然后将此方法包装在Cython中
and then wrap this method in Cython
cdef extern from "..."
shared_ptr[T] Foo_T_doSth[T]()
顺便说一句,在Cython中包装静态方法的推荐方法看起来是(来自 https://groups.google.com/forum/#!topic/cython-users/xaErUq2yY0s )
cdef extern from "...":
cdef Foo_doSth "Foo::doSth"() # not declared as a memeber
(通过指定要用作字符串的实际函数)。这对您没有帮助,因为它无法处理模板。可能是我在您尝试的方式上给了您错误的建议...
(by specifying the actual function to use as a string). This doesn't help you here because it doesn't cope with templates. It may have been me who mis-advised you on the way you were trying...
这篇关于模板类中的Cython C ++静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!