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
不是模板,那麼就沒有錯誤。有關如何正確執行此操作的任何建議?