Boost logo

Boost :

From: Daniel Walker (daniel.j.walker_at_[hidden])
Date: 2008-05-15 18:37:46


On Thu, May 15, 2008 at 5:59 PM, Marco Costalba <mcostalba_at_[hidden]> wrote:
> On Thu, May 15, 2008 at 8:36 PM, Daniel Walker
> <daniel.j.walker_at_[hidden]> wrote:
>>
>> Yes, so long as the copy constructor of the wrapped function object
>> maintains internal_state.
>
> If the copy c'tor copies the internal state then what's the difference between:
>
> function<float(float,float)> add_floats = plus();
>
> add_floats.target<plus>()->internal_state = 7;
>
> function<int(int,int)> f0 = functional_cast<plus(int,int)>(add_floats);
>
> std::cout << f0.internal_state; // it's 7 !
>
>
> And
>
>
> function<float(float,float)> add_floats = plus();
>
> add_floats.target<plus>()->internal_state = 7;
>
> plus tmp( *add_floats.target<plus>() ); // copy c'tor here !
>
> function<int(int,int)> f0 = tmp;
>
> std::cout << f0.target<plus>()->internal_state; // should be 7 the same
>
>
>
>
>
> I would say It seems that expression
>
> function<int(int,int)> f0 = functional_cast<plus(int,int)>(add_floats);
>
> is very similar to
>
> plus tmp( *add_floats.target<plus>() );
>
> function<int(int,int)> f0 = tmp;
>
>
> Is this correct ?

Yes, that's basically all there is to it. Really, it's just ....

function<int(int,int)> f0 = *add_floats.target<plus>();

... with error checking.

functional_cast is almost as simple as lexical_cast. They're both just
syntactic sugar in a way. They communicate the notion that you're
converting between two types that represent the same thing. Check out
the implementation I sent yesterday. functional_cast's body is just
four lines. It's nothing fancy; it's just more expressive and possibly
more convenient than directly manipulating pointers to
boost::function's innards.

Daniel


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