c++ - How to instantiate a template method of a template class with swig? -
i have class in c++ template class, , 1 method on class templated on placeholder
template <class t> class whatever { public: template <class v> void foo(std::vector<v> values); }
when transport class swig file, did
%template(whatever_myt) whatever<myt>;
unfortunately, when try invoke foo
on instance of whatever_myt python, attribute error. thought had instantiate member function with
%template(foo_double) whatever<myt>::foo<double>;
which write in c++, not work (i syntax error)
where problem?
declare instances of member templates first, declare instances of class templates.
example
%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 << " " << << std::endl; } }; %} // member templates %template(fooi) whatever::foo<int>; %template(food) whatever::foo<double>; // class templates. each contain fooi , food members. %template(whateveri) whatever<int>; %template(whateverd) whatever<double>;
output
>>> 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 swig 2.0 documentation.