Boost logo

Boost :

Subject: Re: [boost] Determining interest in Delegates submission
From: Jared McKee (jared.mckee_at_[hidden])
Date: 2011-09-08 11:48:59


On Thu, Sep 8, 2011 at 11:29 AM, Jeffrey Lee Hellrung, Jr.
<jeffrey.hellrung_at_[hidden]> wrote:
> On Thu, Sep 8, 2011 at 7:46 AM, Jared McKee <jared.mckee_at_[hidden]> wrote:
>
>> On Thu, Sep 8, 2011 at 10:19 AM, Jeffrey Lee Hellrung, Jr.
>> <jeffrey.hellrung_at_[hidden]> wrote:
>> >
>> > On Wed, Sep 7, 2011 at 7:48 PM, Jared McKee <jared.mckee_at_[hidden]>
>> wrote:
>> >
>> > > I would like to determine interest in a possible library submission.
>> > >
>> > > I have written some code which converts a member function and object
>> > > pointer
>> > > combination to a free function for use in C style callbacks. It has
>> some
>> > > limitations which I will describe shortly.
>> > >
>> > > The basic idea is to be able to convert:
>> > >
>> > > R (B::*)(T0,T1,…) and B*
>> > >
>> > > to
>> > >
>> > > R (*)(T0,T1,…)
>> > >
>> > > Here's an example usage:
>> > >
>> > > class foo {
>> > >       int x;
>> > >       int test(int v) {return x + v;}
>> > > };
>> > >
>> > > foo* a = new foo;
>> > > a->x = 3;
>> > > int (*f)(int) = delegate(a, &foo::inc);
>> > > cout << f(4) << endl; // outputs "7"
>> > >
>> > [...snip implementation notes...]
>> >
>> > I admit I only glossed over your implementation notes, but it looks like
>> > this would be very similar to (or...the same as...?) a bound (e.g., via
>> > Boost.Bind) boost::mem_fn:
>> >
>> > http://www.boost.org/doc/libs/1_47_0/libs/bind/mem_fn.html
>> >
>> > Is that accurate?
>>
>> Good question. I believe the approach in boost::mem_fn is to create
>> C++ functor objects which are structs with operator() overloaded.
>> These functors are not compatible with straight C function pointers.
>> My implementation returns C function pointers, not functors.
>>
>> Here's a discussion about the topic:
>>
>> http://stackoverflow.com/questions/1840029/passing-functor-as-function-pointer
>>
>
> Oh...dear.
>
> I can see the use for a small module that converts C++ functors into a kind
> of (function pointer, void*) struct for use with interfacing with C-style
> APIs, but trying to get a lonely function pointer to call a functor seems
> wrought with peril (e.g., "gotchas" and safety issues)...  I assume you're
> trying to provide a general solution to the 3rd case within the first answer
> to the referenced stackoverflow link?

That's exactly right.

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


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