If SWIG is given the following code,
%typemap(ctype) SWIGTYPE*& "TESTING*&"
%typemap(ctype) const SWIGTYPE*& "const TESTING*&"
%typemap(cstype) SWIGTYPE *& "ref TESTING"
%typemap(cstype) const SWIGTYPE *& "TESTING"
class Foo{}; // optional
void Test1(const Foo*& x);
It works as it should: the const SWIGTYPE*& typemaps are used. However, if an equivalent function is produced using %template, the non-const SWIGTYPE *& typemaps are used instead, e.g.
template<class T> void Test2(const T& x);
%template(Test2) Test2<Foo*>;
I think this bug is biting me again. Having written typemaps that allow object pointers to be passed by-reference, when I try to make a wrapper for std::vector<MyClass*> (where MyClass is also wrapped by SWIG) certain methods like Add, Insert and setitem come out wrong in C#:
public void Add(ref StringSearchResult x) { ...
public void Insert(int index, ref StringSearchResult x) { ...
private void setitem(int index, ref StringSearchResult val) { ...
I believe this is because SWIG wrongly interprets "const T&" as "StringSearchResult*&" when expanding templates.
Note that in C++ the following two are identical types, likewise in SWIG:
SWIGTYPE const*&
const SWIGTYPE*&
Replace the two cstype typemaps you gave with:
%typemap(cstype) SWIGTYPE *& "ref TESTING"
%typemap(cstype) SWIGTYPE const*& "TESTING1"
%typemap(cstype) SWIGTYPE *const& "TESTING2"
Test1 then uses "TESTING1" and Test2 uses "TESTING2". This is correct as Test2 is more correctly written as:
template<class T> void Test2(T const& x) {}
which when instantiated with T as Foo *, is really Test2(Foo *const&) and so will not match SWIGTYPE const*&.
Incidentally, swig-2.0.0 is much improved in typemap debugging with the -debug-tmsearch and -debug-tmused options documented in the Typemaps section in Typemaps.html section. Currently not released, but available in svn.
Some language modules in older versions also incorrectly had SWIGTYPE *& typemaps when these should have been SWIGTYPE *const&, corrected in 2.0.0. This fixes the C# vector wrappers of const pointers too.