Boost logo

Boost :

From: Daniel Walker (daniel.j.walker_at_[hidden])
Date: 2008-05-15 15:36:49


On Thu, May 15, 2008 at 2:07 AM, Marco Costalba <mcostalba_at_[hidden]> wrote:
> On Thu, May 15, 2008 at 3:06 AM, Daniel Walker
> <daniel.j.walker_at_[hidden]> wrote:
>>
>> I've attached a file that provides a simple unary implementation of
>> all this as a proof of concept and to give an idea of what the
>> boilerplate would look like. It should work for both C++03 and 0x. I
>> tested with gcc 4.3. If there's interest, maybe this and Marco's work
>> and possible parts of Egg could be organized into something really
>> useful.
>>
>
> Hi Daniel,
>
> thanks a lot definitely there is interest, I will take a deep look
> at Egg (that I don't know) and also to your polymorphic_function<>

Cool!

>
> On this I have a naive question, just to clear the things for me.
>
> The point of Giovanni is to use a generalized callback for type
> erasure of underlying function, but when you write
>
> function<int(int,int)> f0
> = functional_cast<plus(int,int)>(add_floats);
> function<float(float,float)> f1
> = functional_cast<plus(float,float)>(add_ints);
>
>
> The fact that the poly class is "plus" it's explicit anyway. IOW
> what's the difference of the above from
>
> function<int(int,int)> f0 = plus();
> function<float(float,float)> f1 = plus();

The assignments to f0 or f1 using functional_cast wraps a copy of the
original plus object wrapped by add_floats or add_ints. The second
assignments wrap brand new plus objects.

>
> Again, in other words, does functional cast support this ?
>
<snip>
> std::cout << f0.internal_state; // if it's 7 then you have made
> something very very interesting

Yes, so long as the copy constructor of the wrapped function object
maintains internal_state. Alternatively, I believe you could use ref()
and the wrapped object would always be the same, not a copy. Also, to
access the members of the wrapped object you need to use
boost::function::target() like so:

std::cout << f0.target<plus>()->internal_state;

functional_cast uses function::target() internally. This API is in the
C++0x standard as well, so functional_cast can work with both
boost::function and std::function; they're more or less the same
anyway.

Daniel Walker


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