
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))); 2) Use Boost.Lambda's bind() and dereference the placeholder directly in the bind expression. for_each(vec.begin(), vec.end(), bind(&foo, *_1)); Here's a complete example demonstrating option 2). #include <vector> #include <algorithm> #include "boost/shared_ptr.hpp" #include "boost/lambda/bind.hpp" #include "boost/lambda/lambda.hpp" class A {}; void foo(const A& a) {} int main(int argc, char** argv) { using boost::lambda::bind; using boost::lambda::_1; std::vector<boost::shared_ptr<A> > vec; vec.push_back(boost::shared_ptr<A>(new A())); for_each(vec.begin(), vec.end(), bind(&foo, *_1)); return 0; } Cheers, Bjorn Karlsson www.skeletonsoftware.net