Boost logo

Boost Users :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2003-09-04 18:03:31


On Thursday 04 September 2003 06:26 pm, Mark Sizer wrote:
> I'm new to boost::bind and boost::mem_fn. I've read the documentation
> and I think I understand what they do, but I'm not sure how to use them
> to accomplish my task.
>
> I have a class that contains several vectors in a map. I want to put a
> "foreach" method on the class that will callback an arbitrary member
> function, of an arbitrary class, for each item in the vector.
>
> I'm stuck on the signature of that method.
[snip]

> void VectorHolder::foreach( std::string& rsName,
> WHAT? callback )
> {
> std::vector<VectorContent>& rvect = rvectGet( rsName );
> std::for_each( rvect.begin(), rvect.end(), callback );
> }

You can write it the signature like this:

  void VectorHolder::foreach( std::string& rsName,
           const boost::function<void (VectorContent&)>& callback)

Or, if you need compatibility with broken compilers,

  void VectorHolder::foreach( std::string& rsName,
           const boost::function1<void, VectorContent&>& callback)

Both caller options (e.g., passing a member function pointer directly and
using boost::mem_fn) will work with this formulation, as will any function
object.

        Doug


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net