1
我有一个模板A类和派生类B:如何在Cython中编写派生模板cpp类的构造函数?
test.hpp
:
#pragma once
namespace test {
template <typename T1>
class A {
T1 a;
public:
A(T1 _a) : a(_a) { }
virtual ~A() { }
};
class B : public A<int> {
public:
B(int a) : A<int>(a) { }
virtual ~B() { }
};
}
它可以编译。
然后我试着写一个用Cython脚本揭露A和B:
test.pyx
:
# distutils: language = c++
cdef extern from "test.hpp" namespace "test":
cdef cppclass A[T1]:
A(T1)
cdef cppclass B(A):
B(int)
然后我得到一个编译错误:
test.pyx:7:10: no matching function for call to A::A()
Traceback (most recent call last):
File "setup.py", line 8, in <module>
ext_modules = cythonize("test.pyx"),
File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 825, in cythonize
cythonize_one(*args[1:])
File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 944, in cythonize_one
raise CompileError(None, pyx_file)
我注意到,如果类A
不是模板,那么就没有错误。有关如何正确执行此操作的任何建议?