【问题标题】:How to create template function in SWIG for c++?如何在 SWIG 中为 c++ 创建模板函数?
【发布时间】:2013-12-23 13:24:56
【问题描述】:

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

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

我想用 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);

【问题讨论】:

    标签: c++ python-2.7 swig


    【解决方案1】:

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

    %template(TfunctionInt) Tfunction<int>;
    

    这在section 6.18 of SWIG docs中有详细解释。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 2019-01-09
      相关资源
      最近更新 更多