Boost logo

Boost :

From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2003-06-05 17:17:07


>From: "Pavel Vozenilek" <pavel_vozenilek_at_[hidden]>

> "Terje Slettebø" <tslettebo_at_[hidden]> wrote in message
> news:3df901c32b88$77439410$8d6c6f50_at_pc...
> [snip]
>
> > int main()
> > {
> > function_ptr<int (A*, int), &A::a_member> fn;
> >
> > // The rest the same
> >
> > A a;
> > int r=fn(&a, 3); // sets r to 9
> > }
> >
> Is it similar (in principle) to
> http://www.code-genie.com/cpp/articles/events/events.html (long text)?

Not quite. The attached code doesn't implement any closure. All it does it
to provide a convenient way of defining a functions, which then calls the
provided member function.

The following:

function_ptr<int (A*, int), &A::a_member> fn;

gets essentially transformed to:

int unique_name(A* c, int a1)
{
  return (c->*&A::a_member)(a1);
}

"fn" is an object which has an implicit conversion to pointer to function,
giving a pointer to "unique_name". "unique_name" is guaranteed to be unique
for each member function pointer, as it uses the member function pointer as
part of its template-id.

It should theoretically be possible to bind arguments this way, as well, so
you implement a kind of closure, but that isn't currently implemented.

You might for example do:

A a;

function_ptr<int (A*, int), &A::a_member> fn(&a, 1);

fn(); // Call (&a->*&A::a_member)(1)

However, in this case, with the current implementation, the bound arguments
would be per-class, not per-object, since they would be stored in the
"unique_name" function.

Regards,

Terje


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