如何在SWIG中为c ++创建模板函数?

时间:2013-12-23 13:24:56

标签: c++ python-2.7 swig

我有一个模板函数,在c ++文件中有以下签名

template <class T> std::vector<T> function(T);  

我想创建一个接口文件,使用Swig将main.cpp包装到python文件中。 我应该如何在Swig中包含Tfunction,对于int float和double类型?

//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);

1 个答案:

答案 0 :(得分:3)

由于Python不知道模板,您必须为每个参数创建一个类型。您可以使用方便的%template指令:

%template(TfunctionInt) Tfunction<int>;

section 6.18 of SWIG docs中详细解释了这一点。