1
votes

I have a template function with following signature in c++ file

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

I wanna make an interface file to wrap main.cpp to a python file with Swig. How should I include Tfunction in Swig, for types int float and 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

1 Answers

3
votes

Since Python does not know templates, you have to create a type for each parameter. There is a convenient %template directive you would use:

%template(TfunctionInt) Tfunction<int>;

This is explained in detail in section 6.18 of SWIG docs.