问题
I make some trials with swig in order to extend basic C++ class to python. I found a behavior related to the use of sets that I can't explain so far. Here are my scripts:
MyClass.h:
#pragma once
#include <set>
#include <vector>
class MyClass
{
public:
MyClass();
void setSTLVector(const std::vector<int> &vector);
void setSTLSet(const std::set<int> &set);
private:
std::vector<int> _stlVector;
std::set<int> _stlSet;
};
MyClass.cpp:
#include "MyClass.h"
MyClass::MyClass()
{
}
void MyClass::setSTLVector(const std::vector<int> &vector)
{
_stlVector = vector;
}
void MyClass::setSTLSet(const std::set<int> &set)
{
_stlSet = set;
}
MyClass.i:
%module MyClass
%{
#include "MyClass.h"
%}
%include <typemaps.i>
%include "std_vector.i"
%template(IntVector) std::vector<int>;
%include "std_set.i"
%template(IntSet) std::set<int>;
%include "MyClass.h"
when compiling everything is (seems) OK. My misunderstanding begins when running my extension into python. Indeed:
In [1]: import MyClass
In [2]: cls = MyClass.MyClass()
In [3]: cls.setSTLVector([1,2,3,4])
works perfectly at least how I expect i.e. the python list of integers
is casted internally to a std::vector<int>
. For the set:
In [1]: import MyClass
In [2]: cls = MyClass.MyClass()
In [3]: cls.setSTLVector({1,2,3,4})
triggers the following error:
TypeError: in method 'MyClass_setSTLSet', argument 2 of type 'std::set< int,std::less< int >,std::allocator< int > > const &'
This error being probably related to another I have when I declare a set using the type I defined in swig:
In [1]: import MyClass
In [2]: cls = MyClass.IntSet({1,2,3,4})
which gives:
NotImplementedError: Wrong number or type of arguments for overloaded function 'new_IntSet'.
Possible C/C++ prototypes are:
std::set< int >::set(std::less< int > const &)
std::set< int >::set()
std::set< int >::set(std::set< int > const &)
Would you have any idea about what I am doing wrong or is that a normal behavior ?
回答1:
The typemaps for std_set.i
unintuitively expect a Python list
as input and not a set
.
>>> import MyClass
>>> cls = MyClass.MyClass()
>>> cls.setSTLVector([1,2,3,4]) # works
>>> cls.setSTLSet([1,2,3,4]) # works
>>> cls.setSTLSet({1,2,3,4}) # doesn't work
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\MyClass.py", line 385, in setSTLSet
return _MyClass.MyClass_setSTLSet(self, set)
TypeError: in method 'MyClass_setSTLSet', argument 2 of type 'std::set< int,std::less< int >,std::allocator< int > > const &'**strong text**
You would have to define your own custom typemap to take a set
as input.
来源:https://stackoverflow.com/questions/60775791/why-swig-casts-python-list-to-stdvector-seamlessly-and-not-stdset