我在 C++ 文件中有一个带有以下签名的模板函数
template <class T> std::vector<T> function(T);
我想制作一个接口(interface)文件,用 Swig 将 main.cpp 包装到 python 文件中。 对于 int float 和 double 类型,我应该如何在 Swig 中包含 Tfunction?
//main.i
%module main
%include "carrays.i"
%array_class(double, doubleArray);
%{
extern template <class T> std::vector<T> Tfunction(T);
%}
extern template <class T> std::vector<T> Tfunction(T);
最佳答案
由于 Python 不知道模板,因此您必须为每个参数创建一个类型。您可以使用一个方便的 %template 指令:
%template(TfunctionInt) Tfunction<int>;
这在 section 6.18 of SWIG docs 中有详细解释。 .
关于c++ - 如何在 SWIG 中为 C++ 创建模板函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20744522/