Boost logo

Boost Users :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-08-12 07:48:09


On Monday 12 August 2002 05:10 am, Sam Partington wrote:
> Am I right in thinking that boost::bind will automatically detect whether
> the function call should be a->f(), a.f() or f(a)?

Bind deals with either function objects or pointers to member functions, so
the forms are: (a.*f)() and (a->*f)() for pointers to member functions, or
f(a) for function objects.

> bool f(const A& a);
>
> std::vector<A> v;
>
> std::vector<A>::const_iterator iter =
> std::find_if(v.begin(), v.end(), boost::bind(&A::f, _1));
>
> Presumably it does the right thing and calls A::f, but how does it do that?

When bind has a pointer-to-member function, it has the return type and the
argument types. Most importantly, it has the type of the object it will be
called with: 'A'

The trick is this: when you call the bind object as f(x), bind is going to try
to determine if 'x' is an A or is derived from A. If so, it can proceed with
the call as (x.*f)(). Otherwise, bind assumes that 'x' is some form of
pointer and uses a 'get_pointer' facility to extract a raw pointer to call
via (get_pointer(x)->*f)().

[All this logic is actually part of mem_fn, not bind; bind just wraps mem_fn
in these cases]

        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