为什么我需要提升蟒蛇矢量索引套房比较操作? [英] why do I need comparison operators in boost python vector indexing suite?

查看:136
本文介绍了为什么我需要提升蟒蛇矢量索引套房比较操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想揭露C ++ code以

I would like to expose C++ code with a

std::vector<A>

要蟒蛇。我的

class A{};

没有实现的比较操作。当我尝试

does not have a comparison operator implemented. When I try

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的定义

I get an error about comparison operators. If I change the definition of A to

class A {
public:
  bool operator==(const A& other) {return false;}
  bool operator!=(const A& other) {return true;}
};

它的工作原理就像一个魅力。

It works like a charm.

为什么需要实施这些比较操作?有什么办法使用 vector_indexing_suite 没有他们?

Why do I need to implement these comparison operators? Is there any way to use the vector_indexing_suite without them?

推荐答案

vector_indexing_suite 实现了一个 __包含__ 成员函数,这就需要一个相等运算符的presence。因此,你的类型必须提供这样一个操作者

vector_indexing_suite implements a __contains__ member function, which requires the presence of an equality operator. As a consequence, your type must provide such an operator.

Boost.Python的沙箱版本通过使用特征来确定什么样的操作都可以在容器上解决这个问题。例如,找到将仅如果值是平等的可比性提供。

The sandbox version of Boost.Python solve this issue by using traits to determine what kind of operations are available on containers. For instance, find will only be provided if the values are equality comparable.

在默认情况下,Boost.Python的考虑所有的价值是平等的可比性和小于可比。由于你的类型不符合这些要求,就需要专门的特点,以指定它所支持的操作:

By default, Boost.Python consider all values to be equality comparable and less-than comparable. Since your type does not meet these requirements, you need to specialize the traits to specify what operations it supports:

namespace indexing {
  template<>
  struct value_traits<A> : public value_traits<int>
  {
    static bool const equality_comparable = false;
    static bool const lessthan_comparable = false;
  };
}

这是记载这里

这篇关于为什么我需要提升蟒蛇矢量索引套房比较操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆