我想公开 C++ 代码
std::vector<A>
到蟒蛇。我的
class A{};
没有实现比较运算符。当我尝试时
BOOST_PYTHON_MODULE(libmyvec)
{
using namespace boost::python;
class_<A>("A");
class_<std::vector<A> >("Avec")
.def(boost::python::vector_indexing_suite<std::vector<A> >());
}
我收到有关比较运算符的错误。如果我将 A 的定义更改为
class A {
public:
bool operator==(const A& other) {return false;}
bool operator!=(const A& other) {return true;}
};
它就像一个魅力。
为什么我需要实现这些比较运算符?有什么办法可以使用vector_indexing_suite
没有他们?
vector_indexing_suite
实现了一个__contains__
成员函数,需要存在相等运算符。因此,您的类型必须提供这样的运算符。
Boost.Python 的沙盒版本通过使用特征来确定容器上可用的操作类型来解决此问题。例如,find
仅当值具有相等可比性时才会提供。
默认情况下,Boost.Python 认为所有值都是相等可比较和小于可比较的。由于您的类型不满足这些要求,因此您需要专门化特征来指定它支持哪些操作:
namespace indexing {
template<>
struct value_traits<A> : public value_traits<int>
{
static bool const equality_comparable = false;
static bool const lessthan_comparable = false;
};
}
这是有记录的here http://boost-sandbox.sourceforge.net/python/indexing/containers.html#ValueTraits.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)