Sign in to follow this  
scottdewald

boost::lambda & boost::function usage

Recommended Posts

Using Boost 1.33.1 I'm trying to learn the boost::lambda library, so I created a few test cases. I wanted to create a std::set of a user defined type (with no operator<) defined. I did it two ways: stored the actual object and stored pointers to the object. I got it to compile both ways, but the const'ness of the boost::function arguments differed in the two. I don't understand why. EDIT: If I remove the const in Option 1 it breaks or if I add const in Option 2 it breaks.
struct Num
{
  Num(int v = 0) : val(v) {}
  int val;
};

// Option 1: storing objects
typedef boost::function<bool(const Num&,const Num&)> compare_t;
compare_t comp = boost::lambda::bind(&Num::val, boost::lambda::_1) < 
                 boost::lambda::bind(&Num::val, boost::lambda::_2);
std::set<Num,compare_t> myset(comp);
myset.insert(Num(3));


// Option 2: storing pointers
typedef boost::function<bool(Num*,Num*)> compare_t;
compare_t comp = boost::lambda::bind(&Num::val, boost::lambda::_1) < 
                 boost::lambda::bind(&Num::val, boost::lambda::_2);
std::set<Num*,compare_t> myset(comp);  
myset.insert(new Num(3));
[Edited by - scottdewald on November 8, 2006 11:28:40 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this