Boost.multi_index: how to use a member function instead ofoperator<()?

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; } struct FnIndex{}; typedef multi_index_container< Object, indexed_by < ordered_unique< identity< Square > >, // this uses operator<(const Object&) I believe? ordered_unique< tag< FnIndex >, ?????????????? >
ObjectContainer;
If this possible, what should I use in the place of ????????? TIA -- Groeten, Joost Kraaijeveld Askesis B.V. Molukkenstraat 14 6524NB Nijmegen tel: 024-3888063 / 06-51855277 fax: 024-3608416 web: www.askesis.nl

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

I don't think you can use a method directly (Boost.bind won't work because you need a type, not a functor instance). I would just create a nested functor class to call Object::fn. Here is an example, compiled on Boost 1.38.0, gcc 4.3.2. I would put real code in Object::fn(), though. There may be an MPL metafunction to do the equivalent of what the struct ordering does. # include <boost/multi_index_container.hpp> # include <boost/multi_index/ordered_index.hpp> class Object { public: struct FN_INDEX; bool fn(Object const& that) const { return true; } struct ordering : public std::binary_function<Object const&, Object const&, bool> { bool operator () (Object const& lhs, Object const& rhs) const { return lhs.fn(rhs); } }; }; using namespace boost; using namespace boost::multi_index; typedef multi_index_container < Object , indexed_by < ordered_unique < identity < Object > > , ordered_unique < tag<Object::FN_INDEX> , identity<Object> , Object::ordering > > > container; container TheContainer; At 01:03 PM 2/23/2009, you wrote:
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; }
struct FnIndex{};
typedef multi_index_container< Object, indexed_by < ordered_unique< identity< Square > >, // this uses operator<(const Object&) I believe? ordered_unique< tag< FnIndex >, ?????????????? >
ObjectContainer;
If this possible, what should I use in the place of ?????????
participants (3)
-
Alan M. Carroll
-
Joaquin M Lopez Munoz
-
Joost Kraaijeveld