小编pio*_*ocz的帖子

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

问题

我在C++中有一个模板类,它有一个静态方法.它看起来或多或少像这样:

template<typename T>
class Foo {
    static std::shared_ptr<Foo<T>> doSth();
}
Run Code Online (Sandbox Code Playgroud)

所以在C++中你会称它为:Foo<Int>::doSth();.但是在Cython中,调用静态方法的方法是使用classname作为命名空间:

cdef extern from "Bar.h" namespace "Bar":
    shared_ptr[Bar] doSth()  # assuming shared_ptr is already declared
Run Code Online (Sandbox Code Playgroud)

但这没有模板的概念.显然,简单地Foo<T>作为命名空间传递是行不通的,因为它转换为Foo<T>::doStr()C++,没有具体的类型代替T.

你会怎么在Cython中做到这一点?有办法还是解决方法?

c++ templates cython

11
推荐指数
2
解决办法
1817
查看次数

Cython:python类包装器中的模板

有没有办法为包含模板的Cython包装的C++类创建Python包装器?(即完全按照此处显示但使用模板:http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#create-cython-wrapper-class).

我知道融合类型的变通方法(https://groups.google.com/forum/#!topic/cython-users/qQpMo3hGQqI),但是这不允许你实现类似的类vector<vector<int>>:融合类型具有,毫不奇怪,没有递归的概念.

改写

我想要实现的是一个包装类,如:

cdef extern from "header.h":
    cdef cppclass Foo[T]:
        Foo(T param)
        # ...
Run Code Online (Sandbox Code Playgroud)

创建一个简单的Python包装器:

cdef class PyFoo[T]:  # I know the '[T]' can't be here, it's a wish
    cdef Foo[T] *thisptr
    def __cinit__(self, param):
        self.thisptr = new Foo[T](param)
    # ...
Run Code Online (Sandbox Code Playgroud)

我很确定Cython本身并不支持,但也许有人可以想到一个解决方法.我不是在寻找惯用的或好的例子,我只是想知道这是否可行.

c++ python templates cython

11
推荐指数
1
解决办法
2050
查看次数

Cython:使用C++流

问题

如何使用Cython中的c ++流(如std::ifstreamostream)?在c ++中,您可以执行以下操作:

std::ofstream output { filename, std::ios::binary };
output.write(...);
Run Code Online (Sandbox Code Playgroud)

你如何在Cython中实现同样的目标?

当前状态

我已经在Cython中包装了fstream中的结构,以便我可以在函数声明中使用它们的名称,但是棘手的部分是使用(在Cython中包装,可能)write方法并创建流.我没有在互联网上找到任何代码示例.

PS我知道一个可能的答案是只使用Python的IO但我需要传递/返回与我正在连接的C++代码的流.

这是包装流声明的代码:

cdef extern from "<iostream>" namespace "std":
    cdef cppclass basic_istream[T]:
        pass

    cdef cppclass basic_ostream[T]:
        pass

    ctypedef basic_istream[char] istream

    ctypedef basic_ostream[char] ostream
Run Code Online (Sandbox Code Playgroud)

c++ python io stream cython

10
推荐指数
1
解决办法
1508
查看次数

标签 统计

c++ ×3

cython ×3

python ×2

templates ×2

io ×1

stream ×1