我如何将我的Python对象类型为
最好的方法类似于这样:示例。不幸的是,代码会崩溃,我似乎无法弄清原因。这是我使用的内容:
ClassName
的列表传递给接受vector<ClassName>
的C++函数?最好的方法类似于这样:示例。不幸的是,代码会崩溃,我似乎无法弄清原因。这是我使用的内容:
template<typename T>
void python_to_vector(boost::python::object o, vector<T>* v) {
try {
object iter_obj = object(handle<>(PyObject_GetIter(o.ptr())));
return;
for (;;) {
object obj = extract<object>(iter_obj.attr("next")());
// Should launch an exception if it cannot extract T
v->emplace_back(extract<T>(obj));
}
} catch(error_already_set) {
PyErr_Clear();
// If there is an exception (no iterator, extract failed or end of the
// list reached), clear it and exit the function
return;
}
}