gpt4 book ai didi

boost-python - pybind11 相当于 boost::python::extract?

转载 作者:行者123 更新时间:2023-12-04 05:11:49 25 4
gpt4 key购买 nike

我正在考虑将复杂代码从 boost::python 移植到 pybind11,但我对缺少 boost::python::extract<...>().check() 之类的东西感到困惑。 .我读了 pybind11::cast<T>可用于从 py::object 中提取 c++ 对象,但检查转换是否可行的唯一方法是调用它并在转换失败时捕获异常。有什么我忽略的吗?

最佳答案

isinstance将完成这项工作( doc ):

namespace py = pybind11;
py::object obj = ...
if (py::isinstance<py::array_t<double>>(obj))
{
....
}
else if (py::isinstance<py::str>(obj))
{
std::string val = obj.cast<std::string>();
std::cout << val << std::endl;
}
else if (py::isinstance<py::list>(obj))
{
...
}

关于boost-python - pybind11 相当于 boost::python::extract?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40423344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com