|
Boost Users : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-09-20 08:11:18
From: "someuserat" <marktutt_at_[hidden]>
> > This is getting harder.
> >
> > bind(&DataMember::equals, bind(&WidgetMap::getIndexedMember, _1), d)
> >
> > would be my guess. Easier would be to define a helper function
> >
> > bool memberEquals(WidgetMap const & m, DataMemberPtr d)
> > {
> > return m.getIndexedMember().equals(d);
> > }
> >
> > and then use
> >
> > bind(memberEquals, _1, d) in dropWidgetMember.
>
> I actually ended up figuring out how to write a function object class
> to do something similar. This has been a good learning experience!
> Is there a down side to the function object approach over the helper
> function? From what I could tell, the helper function had to be a
> global function, which just didn't seem right.
The two approaches are about equal. The function object requires slightly
more effort since you need boilerplate code to hold the state ('d' in the
example); boost::bind is good at holding state. The function object is also
less flexible, the function is more likely to be reusable in other contexts.
On the other hand the function will typically not be inlined, although this
is rarely a concern. Of course you can use a stateless function object
similar to 'memberEquals', too.
Both the function and the helper class can be defined in an unnamed
namespace; the function can be made static. Neither is more global than the
other, so this is not a deciding factor. You can make the function member of
WidgetMap, too, if you prefer the OO style, although this is a bit
controversial. :-) boost::bind just gives you more options; it's up to you
to choose what works best.
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