【发布时间】:2021-02-19 14:39:48
【问题描述】:
在 Python 本身中,您只需编写 float(f),其中 f 是 Python 十进制。这会产生一个 double 类型(在 Python 中称为 float)。
但我需要在boost::python 中执行此操作。我的代码(删节)是
double toDouble(boost::python::object obj)
{
std::string type = boost::python::extract<std::string>(obj.attr("__class__").attr("__name__"));
std::string module = boost::python::extract<std::string>(obj.attr("__class__").attr("__module__"));
std::string cls = module + "." + type;
if (cls == "decimal.Decimal"){
double f = boost::python::extract<double>(obj);
return f;
}
throw "Oops";
}
但我得到一个错误
没有注册的转换器能够产生类型的 C++ 右值 来自这个十进制类型的 Python 对象的 double.Decimal
我该怎么做?我无法想象它很复杂。显然我错过了一些东西。
【问题讨论】:
标签: c++ boost-python