Boost logo

Boost :

From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2002-07-08 17:49:18


> "Paul Mensonides" <pmenso57_at_[hidden]> wrote in message
> news:001201c226c7$e2cb0ae0$7772e50c_at_c161550a...
> > [...]
> > It is possible to implement a closure facility to handle this. The
> > preprocessor library can generate the necessary specializations
> > to support various pointer-to-member arities.
> > [...]
>
> So you're saying that implementing operator->* basically requires
> closures because of the return type? I guess I didn't think about it,
> but that makes sense. I wonder if there is an easy way to hijack
> boost::function to help here?
>
> Dave

Not really because of the return type per se. The problem is
pointers-to-member-functions. When you overload the ->* in this expression:

(obj->*ptm)(1, 2, 3);

You don't get (1, 2, 3). You only get 'ptm'. So, you have to return a closure
that stores the object pointer and the pointer-to-member function and has a
operator() that can handle the right number of arguments of the right type.

This is only the case with pointers-to-member-functions though. In the sample
that I sent previously, the mechanism hijacks the case where its a
pointer-to-data-member and returns the result directly.

In essence though, this really *is* a closure facility that can be used outside
of smart pointers:

struct X {
    int f(int, int);
};

int main(void) {
    X* px = new X;
    typedef int (X::* pf_t)(int, int);

    pf_t pf = &X::f;

    closure<pf_t> cl(px, pf);

    // ...

    cl(2, 3);

    // ...

    delete px;
    return 0;
}

Paul Mensonides


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