Boost logo

Boost-Build :

From: Preston A. Elder (prez_at_[hidden])
Date: 2007-09-26 05:58:47

I have written code to solve this problem:

I have class foo, like so:

class foo
    boost::function<void (foo *)> func_ptr;

    void call()

I want func_ptr to be either a free-standing function (or class static), OR
a class member function of a derived class, so:

void bar(foo *f) { ... }


class baz : public foo
       func_ptr = &baz::doit;

    void doit() { }

The obvious problem with the latter is that it needs to be downcast
implicitly, and some kind of exception thrown if the wrong type is passed.

I have created a boost::function proxy class to handle exactly this
problem. It uses a lot of boost::function's own tricks with the
pre-processor and such. I also had to use another suggestion I saw on
this list to work around the fact boost::function does not accept member
function pointer syntax (argh! That took a lot to figure out).

My solution is this:

Which is included by a boost preprocessor iteration here:

As I mention, I use a lot of boost::function's own tricks. I think this
would be a lot cleaner if done as part of boost::function, rather than me
re-implementing the same macros and such.

I have made sure my implementation works, and more importantly, has all
the tests you would expect (ie. that the return value arguments except the
class are the same, that the class is derived from the original function
typedef's first argument, and so on).

Here is my test case:

Any way, feedback is as always appreciated, but if someone could look at
this for an addition to boost::function it would really be great :)

PreZ :)

Boost-Build list run by bdawes at, david.abrahams at, gregod at, cpdaniel at, john at