Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using boost::bind with a constructor

I'm trying to create new objects and add them to a list of objects using boost::bind. For example.

struct Stuff {int some_member;};
struct Object{
    Object(int n);
};
....
list<Stuff> a;   
list<Object> objs;
....
transform(a.begin(),a.end(),back_inserter(objs), 
  boost::bind(Object,
     boost::bind(&Stuff::some_member,_1)
  )
);

This doesn't appear to work. Is there any way to use a constructor with boost::bind, or should I try some other method?

like image 348
Dan Hook Avatar asked Aug 26 '09 14:08

Dan Hook


1 Answers

If you are using boost 1.43, you can use boost::factory and boost::value_factory, which let you encapsulate a constructor invocation. Like this:

 transform(a.begin(),a.end(),back_inserter(objs), 
  boost::bind(boost::value_factory<Object>(),
     boost::bind(&Stuff::some_member,_1)
  )
);
like image 108
syffinx Avatar answered Oct 06 '22 13:10

syffinx