[C++-sig] A vector of objects with private == operators

Andreas Klöckner lists at informa.tiker.net
Wed Mar 26 19:42:55 CET 2008


On Mittwoch 26 März 2008, Matthew Scouten wrote:
> I have noticed that the vector indexing suite only compiles for a
> vector<foo> if foo has an == operator. Some of the objects that I need to
> wrap in a library have to wrap have no == operator. That's fine, I just
> create a dummy operator as a global function. The dummy operator just tests
> &a==&b.
>
> Now I come to a problem. the 'bar' class has a == operator, but it is
> declared private. I tried to create a global == operator but the compiler
> complained that it was ambiguous (the compiler is such a whiny little
> brat.)
>
> So my questions are 3 fold,
> Why the heck does  vector_indexing_suite need an == operator?
> Assuming I can't change the problem class, what are possible workarounds?
> Does vector_indexing_suite2 still need an == operator?

This is a hack I've been using:

8< --------------------------------------------------------------
template <class T>
class no_compare_indexing_suite :
  public boost::python::vector_indexing_suite<T, false, 
no_compare_indexing_suite<T> >
{
  public:
    static bool contains(T &container, typename T::value_type const &key)
    { PYTHON_ERROR(NotImplementedError, "containment checking not supported on 
this container"); }
};
8< --------------------------------------------------------------

Use like so:

cl.def(no_compare_indexing_suite<container_type>())

Andreas
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080326/1cdc4312/attachment.pgp>


More information about the Cplusplus-sig mailing list