无法用swig包装模板静态函数

时间:2021-09-27 21:24:00

I am trying to wrap a simple static template function like this:

我试图包装一个简单的静态模板函数,如下所示:

namespace ns {
  class A {
   public:
    template<typename T>
    static void func() {}
  };
}

with the swig directive:

使用swig指令:

%template(func_type) ns::A::func<type>;

But I keep getting:

但我一直在:

Error: Undefined scope 'ns::A'
Error: Template 'ns::A::func' undefined.

I have found the same issue asked on this mailing list, but unfortunately there are no replies. Is this a swig limitation? Does it require some special operation?

我在此邮件列表中发现了同样的问题,但遗憾的是没有回复。这是一个swig限制吗?它需要一些特殊操作吗?


Here are the few lines above ready to be pasted in .i file:

以下几行准备粘贴在.i文件中:

%module example
%{
namespace ns {
  class A {
   public:
    template<typename T>
    static void func() {}
  };
}
%}

%template(func_int) ns::A::func<int>;

To process this with swig just do:

要使用swig进行处理,请执行以下操作:

swig -c++ -tcl test.i

2 个解决方案

#1


0  

By now I solved this declaring some dummy functions that swig can manage better:

到现在为止,我解决了这个声明一些虚拟函数,swig可以更好地管理:

namespace ns {
  class A {
   public:
    template<typename T>
    static void func() {}

    static void funcT1() {func<T1>();}
    static void funcT2() {func<T2>();}
  };
}

I think it's clean enough, but still I am looking for a more proper solution.

我认为它足够干净,但我仍在寻找更合适的解决方案。

#2


-1  

You have to give it a specific type:

你必须给它一个特定的类型:

%template(func_int) ns::A::func<int>;
%template(func_float) ns::A::func<float>;

You might also (can't test here) have to wrap the %template with namespace:

您也可能(无法在此测试)必须使用命名空间包装%模板:

namespace ns {
    %template(func_int)   A::func<int>;
    %template(func_float) A::func<float>;
}

#1


0  

By now I solved this declaring some dummy functions that swig can manage better:

到现在为止,我解决了这个声明一些虚拟函数,swig可以更好地管理:

namespace ns {
  class A {
   public:
    template<typename T>
    static void func() {}

    static void funcT1() {func<T1>();}
    static void funcT2() {func<T2>();}
  };
}

I think it's clean enough, but still I am looking for a more proper solution.

我认为它足够干净,但我仍在寻找更合适的解决方案。

#2


-1  

You have to give it a specific type:

你必须给它一个特定的类型:

%template(func_int) ns::A::func<int>;
%template(func_float) ns::A::func<float>;

You might also (can't test here) have to wrap the %template with namespace:

您也可能(无法在此测试)必须使用命名空间包装%模板:

namespace ns {
    %template(func_int)   A::func<int>;
    %template(func_float) A::func<float>;
}