Python 返回c+中std::variant的numpy数组+;
我试图创建一个pybd11接口,它将两个编号数组的元组从C++返回到Python。lambda表达式中的子函数lttb返回两个类型为Python 返回c+中std::variant的numpy数组+;,python,c++,numpy,pybind11,Python,C++,Numpy,Pybind11,我试图创建一个pybd11接口,它将两个编号数组的元组从C++返回到Python。lambda表达式中的子函数lttb返回两个类型为std::vector和std::vector的向量 如果我直接返回向量,那么pybind11/stl.h头可以很好地处理转换。创建doublenumpy数组也可以完美地工作。但是,当尝试创建std::variant的numpy数组时,我得到以下错误 ..\subprojects\pybind11\include\pybind11/numpy.h(1159): er
std::vector
和std::vector
的向量
如果我直接返回向量,那么pybind11/stl.h
头可以很好地处理转换。创建double
numpy数组也可以完美地工作。但是,当尝试创建std::variant
的numpy数组时,我得到以下错误
..\subprojects\pybind11\include\pybind11/numpy.h(1159): error C2338: Attempt to use a non-POD or unimplemented POD type as a numpy dtype
有一种方法可以注册结构化类型(),以防止出现此错误消息,但是我只看到一种方法可以使用struct
而不是std::variant
来实现此功能。有人知道如何创建std::variant
的numpy数组吗?应该没有那么难,因为转换std::vector
是现成的。这是密码
.def("lttb",
[&](ScopeDataElement& data,
const std::pair<double, double>& limit,
int64_t masterTick,
long double samplingTime,
size_t destinationSize) {
auto&& [x, y] =
data.lttb(limit, masterTick, samplingTime, destinationSize);
using T = std::decay_t<decltype(x)>::value_type;
using U = std::decay_t<decltype(y)>::value_type;
auto arrx = py::array_t<T>(x.size(), x.data());
auto arry = py::array_t<U>(y.size(), y.data());
return py::make_tuple(arrx, arry);
})
.def(“lttb”,
[&](范围数据元素和数据,
常数标准::对和限制,
int64_t masterTick,
长的双采样时间,
尺寸(目标尺寸){
自动&[x,y]=
数据.lttb(限制、masterTick、采样时间、目标大小);
使用T=std::decage\u T::value\u type;
使用U=std::decage\U t::value\U type;
auto arrx=py::array_t(x.size(),x.data());
auto arry=py::array_t(y.size(),y.data());
返回py::make_tuple(arrx,arry);
})