如何使用swig实例化模板类的模板方法
How to instantiate a template method of a template class with swig?
我在c++中有一个模板类,这个类的一个方法被模板化为另一个占位符
template <class T>
class Whatever {
public:
template <class V>
void foo(std::vector<V> values);
}
当我将这个类传输到swig文件时,我做了
%template(Whatever_MyT) Whatever<MyT>;
不幸的是,当我试图在python的Whatever_MyT实例上调用foo
时,我得到了一个属性错误。我认为我必须用
%template(foo_double) Whatever<MyT>::foo<double>;
这是我在c++中写的,但它不起作用(我得到一个语法错误)
问题在哪里?
先声明成员模板的实例,再声明类模板的实例。
例子%module x
%inline %{
#include<iostream>
template<class T> class Whatever
{
T m;
public:
Whatever(T a) : m(a) {}
template<class V> void foo(V a) { std::cout << m << " " << a << std::endl; }
};
%}
// member templates
// NOTE: You *can* use the same name for member templates,
// which is useful if you have a lot of types to support.
%template(fooi) Whatever::foo<int>;
%template(food) Whatever::foo<double>;
// class templates. Each will contain fooi and food members.
// NOTE: You *can't* use the same template name for the classes.
%template(Whateveri) Whatever<int>;
%template(Whateverd) Whatever<double>;
输出>>> import x
>>> wi=x.Whateveri(5)
>>> wd=x.Whateverd(2.5)
>>> wi.fooi(7)
5 7
>>> wd.fooi(7)
2.5 7
>>> wi.food(2.5)
5 2.5
>>> wd.food(2.5)
2.5 2.5
Reference: 6.18 Templates (search "member template") in the SWIG 2.0 Documentation.
相关文章:
- 使用 SFINAE 设计模板方法
- 在 C++ 中使用模板方法重写类方法
- 使用模板方法为标头提供空 cpp 时出现问题
- MSVC使用constexpr-if从可变模板方法中的基本模板参数中吞下const
- 在模板化类的方法上使用 std::enable_if
- 在任何类类型上使用模板方法中的 new
- 如何使用 gmock 模拟类中的模板方法
- 通常应用方法,使用带有构造函数委托的 SFINAE 通过类模板的构造函数初始化成员
- 在模板中专门使用一些方法
- 是否可以在另一个方法中使用模板类中定义的虚拟方法
- 如何使用类型专用化模板方法,该类型本身就是一个模板,其中只有返回类型依赖于模板类型
- 在模板类中使用模板方法
- 模板化方法中使用的嵌套类的未解析外部符号
- 最有cpu效率的方法是使用std::算法和变量函数模板的参数
- 在模板类中:使用类方法作为另一类方法的模板参数
- C++-对任何类型的模板类都使用相同的静态方法
- 在类接口内的模板化静态方法中使用std::generator_canonical
- 如何使用"using"声明模板基类的模板方法?
- C++:直接使用派生类型的模板方法模式
- 如何初始化模板方法中使用的静态类成员