>From: "Pavel Vozenilek" <[EMAIL PROTECTED]>

> "Terje Slettebø" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> [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

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Reply via email to