
2009/8/18 Björn Karlsson <Bjorn.Karlsson@readsoft.com>
Hello Rob,
Subject: [Boost-users] [bind] How do I....?
Use bind to call a free function on a vector of shared_ptrs?
struct A { }; void f(const A&); std::vector<boost::shared_ptr<A> > v;
for_each(v.begin(), v.end(), boost::bind(f, ???? ) );
what goes where ???? is?
You have two options if you want to use binders:
1) Use Boost.Bind with nested binds, binding operator* of shared_ptr in the inner bind.
for_each(vec.begin(), vec.end(), boost::bind(&foo, boost::bind(&boost::shared_ptr<A>::operator*, _1)));
This was about the best I could think of too, and is, I think, what I'll do.
2) Use Boost.Lambda's bind() and dereference the placeholder directly in the bind expression.
for_each(vec.begin(), vec.end(), bind(&foo, *_1));
Much as I like Lambda for virgin code, in my work code base there's a very visible inclusion of Boost.Bind, with placeholders in the global namespace. I've concluded that the pain of clashing placeholders just isn't worth it, so I steer clear of Lambda now. Thanks Bjorn. - Rob.