I’ve created a C++ library and have successfully used swig to make it accessible through python. Templating is used heavily in the library and each template type is mapped to its own python class by swig like so:
%template(Imageint) Image<int>;
%template(Imagedouble) Image<double>;
However, I would really like to have a ‘default’ template used by python such that
a = Image("filename")
instantiates Image<double> without having to always type
a = Imagedouble("filename")
Swig documentation states:
The %template directive should not be
used to wrap the same template
instantiation more than once in the
same scope. This will generate an
error. This error is caused because
the template expansion results in two
identical classes with the same name.
This generates a symbol table
conflict. Besides, it probably more
efficient to only wrap a specific
instantiation only once in order to
reduce the potential for code bloat.
So to avoid the symbol table conflict, I tried
%rename(Image) Image<double>;
%template(Imageint) Image<int>;
%template(Imagedouble) Image<double>;
in the interface file. However, swig then complains about Image being redefined.
What is the best way to make an alias such that Image and Imagedouble both refer to C++ Image<double>? Thank you very much for any help you can provide.
-Josh
In case this helps someone in the future, the way to do what is describe above is to add the following to the interface file:
I did not realize there was a way to write standard python code in the interface file.