融合类型的C ++类 [英] c++ class in fused type

查看:78
本文介绍了融合类型的C ++类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望为一堆c ++类实现python包装器。我在pxd中某处有

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()

我想知道是否可以在pyx python包装器中编写如下内容:

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()

是否可以完成预期的行为? FooImpl1和FooImpl2不共享抽象接口,它们是类的模板专业化。

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

推荐答案

此版本(0.20)起,Cython不支持类中的融合类型,仅支持函数参数和变量。 这里是文档。

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

这篇关于融合类型的C ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆