【问题标题】:Wrapping Inherited Templates in Cython?在 Cython 中包装继承的模板?
【发布时间】:2019-03-10 23:09:10
【问题描述】:

我正在尝试为一些模板化的 C++ 代码编写一个 cython 包装器。我对包装类似 Bar 的东西非常熟悉。即(删除细节)

 ctypedef enum enum_t "enum_t":
    OPT1 "OPT1",
    OPT2 "OPT2",
    OPT3 "OPT3"

 cdef cppclass Bar[A, B, T, ALLOCATOR=*] 

 new Bar[OPT1, OPT2, float]

但我无法理解包装下面定义的 Bar<OPT1, OPT2, T> or Bar<OPT3, OPT4, T> 之类的实例的做法是什么。有人能指出我正确的方向吗?我所尝试的在编译时给了我一个“OPT1 is ambiguous”错误。

typedef enum
{
  OPT1, 
  OPT2,
  OPT3,
} enum_t;

template<class T, class Allocator = std::allocator<T> >
class BarBase : public Foo<T, Allocator>, public mpi::MPIObject
{
  //Generic class methods and variables
}

template<enum_t A, enum_t B, class T, class Allocator = std::allocator<T> >
    class Bar : public BarBase<T, Allocator>
    {
      public:
      private:
    };

template<typename T>
class Bar<OPT1, OPT2, T> : public BarBase<T>
    {
      //Specific class methods here
    }

template<typename T>
class Bar<OPT3, OPT4, T> : public BarBase<T>
    {
      //Specific class methods here
    }

【问题讨论】:

    标签: c++ inheritance cython wrapper templating


    【解决方案1】:

    Cython 并不真正支持非类型模板参数。在 previous questions when using int template parameters I've recommended using "fake" classes 中签名的名称生成正确的 C++ 代码。同样的原则也适用于这里:

    cdef extern from "X.hpp":        
        cdef cppclass OPT1:
            pass
        cdef cppclass OPT2:
            pass
    
        cdef cppclass Bar[A, B, T]:
            pass
    
    def f():
        cdef Bar[OPT1,OPT2,float]* p = new Bar[OPT1,OPT2,float]()
    

    我只是告诉 Cython OPT1OPT2 是类而不是枚举值,这会欺骗 Cython 生成正确的代码。

    【讨论】:

      猜你喜欢
      • 2019-03-27
      • 2020-01-10
      • 2012-02-01
      • 2019-05-28
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多