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

标签 c++ python-2.7 swig

我在 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/

相关文章:

python - 使用 write_shp 在 NetworkX 中写入 shapefile

c++ - 如何使用boost.pool实现类似STL map的map

C++ - 一个非常简单的 HTTP 服务器 : WSA unreferenced error

python - Pandas :分组并在一个组内切割

python - SWIG 将 C 库连接到 Python(SWIG 生成的类使用起来很麻烦)

python - OS X El Capitan 上的 dlopen 错误 "unsafe use of relative rpath"

c# - 将 QuantLib 转换为 QuantLib-SWIG C#

c++ - 对于下面给出的任务,使用类还是多维数组更好?

c# - 如何调试DLL里面的代码

python - flask 导入错误