Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2001-05-07 18:19:44


On Monday 07 May 2001 03:28 pm, you wrote:
> Douglas Gregor wrote:
> > > I have no idea what 'set, clear, empty' etc
> > > might be for: functions can't be set, cleared,
> > > or emptied. They should act like values, and be
> > > callable.
> > >
> > > {Have I missed something deep here?]
> >
> > Prefix 'function' with 'boost::' and you've missed something :)
> > boost::function is a generalized function object adaptor that is declared
> > with some given function signature and that can contain any function
> > object with a compatible signature. It overloads the function call
> > operator to call the underlying function object's function call operator.
> > One of its uses (the one we're debating currently) is as a callback.
>
> Are you saying that 'set', for example, is to be used to
> change the object to which the adaptor delegates?

Yes.

> So for example, if an adaptor f were registered as a callback,
> the actual callback can be dynamically changed without
> changing the object registered?

I'm tripping over the word 'registered' here. Assuming that it is 'declared'
in the C++ context, yes.

Perhaps code would better illustrate what I mean:

int add(int x, int y) { return x+y; }
int mul(long x, long y) { return x+y; }

boost::function<int, int, int> f;

f = &add;
cout << f(2, 3) << endl; // Writes 5
f = &mul;
cout << f(2, 3) << endl; // Writes 6

So essentially boost::function is a function adaptor that gives a common
static interface (here, a function object taking two ints and returning an
int) to any of the set of function objects (function pointers are merely a
type of function object) with compatible signatures. Here, both "add" and
"mul" have compatible signatures, so they can be targets of the function f.

        Doug


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk