c++ class in fused type

强颜欢笑 提交于 2020-01-21 12:13:26

问题


I wish to implement python wrapper for a bunch of c++ classes. Somewhere in pxd I have:

cdef cppclass FooImpl1:
    FooImpl1()
    int foo()

cdef cppclass FooImpl2
    FooImpl2()
    int foo()

I wonder if I can write something like this in pyx python wrapper:

ctypedef fused FooImpl:
    FooImpl1*
    FooImpl2*

cdef class Foo:
    cdef FooImpl impl
    def __cinit__(self, int selector):
        if selector == 1:
            self.impl = new FooImpl1()
        else:
            self.impl = new FooImpl2()

    def func(self):
        # depending on the object stored in impl FooImpl2::foo or FooImpl1::foo
        # will be called
        return self.impl.foo()

Is there a way to accomplish expected behavior? FooImpl1 and FooImpl2 don't share abstract interface, they are template specializations of a class.


回答1:


As of this version (0.20), Cython doesn't support fused types in classes, only in function parameters and variables. Here are the docs.



来源:https://stackoverflow.com/questions/13400469/c-class-in-fused-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!