
From: przemyslaw.sliwa@uk.bnpparibas.com
<snip>
In order to sort the objects in the set I have to use the functor
struct lessThan : public std::binary_function, boost::shared_ptr, bool> { bool operator ()(const boost::shared_ptr &lhs, const boost::shared_ptr &rhs) const { return (*lhs) < (*rhs); } };
This is quite long and I believe boost can shorten it a lot. I have tried it with boost::lamba and boost::bind but could not make it working. Basically I would like to eliminate the need of writing the definition of struct lessThan. Could someone help me with that?
Here's an alternate solution using boost::lambda and BOOST_TYPEOF. #include <boost/lambda/lambda.hpp> #include <boost/typeof/typeof.hpp> using namespace boost::lambda; typedef BOOST_TYPEOF(*_1 < *_2) lambda_type; typedef std::set<eventIntLimPtr, lambda_type> eventIntLimPtrSet; The only wrinkle is that the type of the lambda *_1 < *_2 does not default construct to give you *_1 < *_2, so you will have to give *_1 < *_2 as an extra constructor parameter every time you construct an eventIntLimPtrSet. If that really bothers you, you can write a derived class with forwarded constructors that put in the extra argument for you: struct eventIntLimPtrSet : std::set<eventIntLimPtr, lambda_type> { typedef std::set<eventIntLimPtr, lambda_type> base_type; eventIntLimPtrSet() : base_type(*_1 < *_2) {} // Other forwarded set constructors }; Regards, Nate.