1

The default typemap provided by swig expects a python list as input for constructing a set instead of native Python set (see here). Indeed,

%include "std_set.i"
%template(IntSet) std::set<int>;

will trigger the following behavior in python:

>>> import MyClass
>>> MyClass.IntSet([1,2,3,4]) # works
>>> MyClass.IntSet({1,2,3,4}) # does not work

I tried to create a custom typemap to do so but I failed so far. Here my current swig file to do so:

%module MyClass

%{
    #include "MyClass.h"
%}

%include <typemaps.i>

%typemap(in) setin {$1 = PySequence_List($input);}
%typemap(out) setout {$result = PySet_New($1);}

%include "std_set.i"
%template(IntSet) std::set<int>;

%include "MyClass.h"

Would you know how to make it ?

Eurydice
  • 8,001
  • 4
  • 24
  • 37

0 Answers0