|
Boost Users : |
Subject: Re: [Boost-users] Using boost::shared_ptr with std::set
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2011-04-04 16:46:11
> From: przemyslaw.sliwa_at_[hidden]
>
> <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.
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