Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost.multi_index: how to use a member function instead ofoperator<()?
From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2009-02-23 15:22:12


Joost Kraaijeveld <J.Kraaijeveld <at> Askesis.nl> writes:

>
> Hi,
>
> I want to use in index based on a member function that returns a bool if
> an element is smaller than an other element like operator<. But I cannot
> find how to do that.
>
> e.g.
>
> using namespace ::boost;
> using namespace ::boost::multi_index;
>
> class Object{
> bool operator<(const Object& rhs) const;
> bool fn(const Object& rhs) const;
> }
>
[...]

Do as follows:

  struct ObjectLessbyFn
  {
    bool operator()(const Object& lhs, const Object& rhs) const{
      return lhs.fn(rhs);
    }
  };

  typedef multi_index_container<
    Object,
    indexed_by<
      ordered_unique< identity< Object > >,
      ordered_unique< tag< FnIndex >, identity< Object >, ObjectLessbyFn >
>
> ObjectContainer;

In the first index std::less<Object> is used as the default
compare predicate, which, as you correctly guess, resorts to
Object::operator<.

Best regards,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net