In a C++ library that I'm not allowed to change I have a constructor that looks like this:
Dfa(const int n_state, const int dim_alf, const string *alf);
If I simply bind with
.def(py::init<const int, const int, const std::string*>())
it compiles succesfully. The problem is that I can't pass a string* by python, because for example if I try to execute on python
alph=['x','y']
z=Dfa(3,2,alph)
It returns the following error:
TypeError: __init__(): incompatible constructor arguments. The
following argument types are supported:
gi_gipy.Dfa(arg0: int, arg1: int, arg2: unicode)
User "R zu" kindly suggested me to write a wrapper, but I can't figure out how. Given that what in python is something like: ['x','y']
,
in c++ is accepted as std::list<std::string>
, I tried writing the following code:
.def(py::init([](int n_state,int dim_alf, std::list<std::string> alph){
std::string* alfabeto=new std::string[dim_alf];
std::list<string>::iterator it=alph.begin();
for(int i=0;it!=alph.end();++i,++it) alfabeto[i]=*it;
Dfa::Dfa(n_state,dim_alf,alfabeto);
}))
but it returns to me 2 errors:
cannot pass expression of type 'void' to variadic function
construct<Class>(v_h, func(std::forward<Args>(args)...)
and
static_assert failed "pybind11::init(): init function must return a compatible pointer,
holder, or value"
static_assert(!std::is_same<Class, Class>::value /* always false */
It is clear that I'm a bit confused on how to overcome this problem, that I think is connected to the use of a pointer to string as a parameter to a constructor. I repeat that I can't change the library, I can only create the appropriate binding. Thank you for your attention
main.cpp:
CMakeLists.txt:
Python 3 test: