|
Boost : |
From: Marco Costalba (mcostalba_at_[hidden])
Date: 2008-05-16 01:36:29
On Thu, May 15, 2008 at 11:37 PM, Daniel Walker
<daniel.j.walker_at_[hidden]> wrote:
>
> 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.
>
I've checked out now. I didn't wanted to look before because I wanted
to try to understand myself, it helps in my understanding when reading
the actual code ;-)...that I have to say is very nice. I've learnt a
lot of tricks from that few lines.
One thing. Regarding the actual core
// Cast between two boost::functions with different call signatures
// wrapping the same polymorphic function object.
template<class Signature, class CallSignature>
function<typename call_signature<signature<Signature> >::type>
functional_cast(function<CallSignature> f)
{
typedef typename callable_object<Signature>::type functor_type;
functor_type const* p = f.template target<functor_type>();
if(!p) throw std::bad_cast();
return function<typename call_signature<signature<Signature> >::type>(*p);
}
I would say that for it to be equivalent to
function<int(int,int)> f0 = *add_floats.target<plus>();
It may be
return function<typename call_signature<signature<Signature>
>::type>(boost::ref(*p));
instead of
return function<typename call_signature<signature<Signature> >::type>(*p);
because boost::function makes a copy as default, so you have one copy
of *p in the boost::function c'tor in the return type and another,
outside functional_cast in the assignment
function<int(int,int)> f0 = functional_cast<...>(..);
Marco
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk