boost::lambda & boost::function usage

Started by
-1 comments, last by scottdewald 17 years, 5 months ago
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]
<a href="http://www.slimcalcs.com>www.slimcalcs.com

This topic is closed to new replies.

Advertisement