Why do we need comparison operators in boost python index indexing package?

I would like to show C ++ code with

std::vector<A> 

for python. My

 class A{}; 

the comparison operator is not 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> >()); } 

I get error messages from 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.

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

+6
source share
1 answer

vector_indexing_suite implements the __contains__ member __contains__ , which requires an equality operator. As a result, your type should provide such an operator.

The sandware version of Boost.Python solves this problem by using traits to determine which operations are available for containers. For example, find will only be provided if the values ​​are equal to equal.

By default, Boost.Python assumes that all values ​​are equal to comparable and less comparable. Since your type does not meet these requirements, you need to specialize functions to indicate which 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; }; } 

This is documented here .

+5
source

Source: https://habr.com/ru/post/916225/


All Articles