The %implict macro allows a SwigType to be accepted
as an input parameter and use its implicit constructors when needed.
%implicit(A, int, double, B);
A(const B& b) { ii = 3; }
int get(A a) { return a.ii; }
Here, you can call 'get' as
and swig will construct an 'A' temporal variable using the
corresponding implicit constructor.
The plain implicit macro takes care of simple type list. If it doesn't
work because you are passing template types with commas, then use
the %implicit_{1,2,3} versions, and the SWIG_arg macro.
%define %implicit_type(...)
%traits_swigtype(__VA_ARGS__);
%define %implicit_frag(...) ,fragment=SWIG_Traits_frag(__VA_ARGS__) %enddef
%define %implicit_code(...)
if (swig::check<__VA_ARGS__ >(obj)) {
if (val) *val = new value_type(swig::as<__VA_ARGS__ >(obj));
%define %implicit(Type, ...)
%formacro_1(%implicit_type,__VA_ARGS__);
%fragment(SWIG_Traits_frag(Type),"header",
%formacro_1(%implicit_frag,__VA_ARGS__)) %{
template <> struct traits<Type > {
typedef pointer_category category;
static const char* type_name() { return "Type"; }
template <> struct traits_asptr< Type > {
static int asptr(PyObject *obj, value_type **val) {
static swig_type_info* desc = SWIG_TypeQuery("Type *");
if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) != -1)) {
if (PyErr_Occurred()) PyErr_Clear();
%formacro_1(%implicit_code,__VA_ARGS__)
PyErr_SetString(PyExc_TypeError, "a " "Type" " is expected");
%typemap_traits_ptr(SWIG_CCode(POINTER),Type);
%define %implicit_1(Type, Imp1)
%fragment(SWIG_Traits_frag(Type),"header",
fragment=SWIG_Traits_frag(Imp1)) %{
template <> struct traits< Type > {
typedef pointer_category category;
static const char* type_name() { return "Type"; }
template <> struct traits_asptr< Type > {
static int asptr(PyObject *obj, value_type **val) {
static swig_type_info* desc = SWIG_TypeQuery("Type *");
if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) != -1)) {
if (PyErr_Occurred()) PyErr_Clear();
PyErr_SetString(PyExc_TypeError, "a " "Type" " is expected");
%typemap_traits_ptr(SWIG_CCode(POINTER),Type);
%define %implicit_2(Type, Imp1, Imp2)
%fragment(SWIG_Traits_frag(Type),"header",
fragment=SWIG_Traits_frag(Imp1),
fragment=SWIG_Traits_frag(Imp2)) %{
template <> struct traits< Type > {
typedef pointer_category category;
static const char* type_name() { return "Type"; }
template <> struct traits_asptr< Type > {
static int asptr(PyObject *obj, value_type **val) {
static swig_type_info* desc = SWIG_TypeQuery("Type *");
if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) != -1)) {
if (PyErr_Occurred()) PyErr_Clear();
PyErr_SetString(PyExc_TypeError, "a " "Type" " is expected");
%typemap_traits_ptr(SWIG_CCode(POINTER),Type);
%define %implicit_3(Type, Imp1, Imp2, Imp3)
%fragment(SWIG_Traits_frag(Type),"header",
fragment=SWIG_Traits_frag(Imp1),
fragment=SWIG_Traits_frag(Imp2),
fragment=SWIG_Traits_frag(Imp3)) %{
template <> struct traits< Type > {
typedef pointer_category category;
static const char* type_name() { return "Type"; }
template <> struct traits_asptr< Type > {
static int asptr(PyObject *obj, value_type **val) {
static swig_type_info* desc = SWIG_TypeQuery("Type *");
if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) != -1)) {
if (PyErr_Occurred()) PyErr_Clear();
PyErr_SetString(PyExc_TypeError, "a " "Type" " is expected");
%typemap_traits_ptr(SWIG_CCode(POINTER),Type);