Boost logo

Boost :

Subject: Re: [boost] [functional] [overloaded_function] polymorphic functions?
From: John Bytheway (jbytheway+boost_at_[hidden])
Date: 2012-02-18 06:24:43


On 18/02/12 03:16, Lorenzo Caminiti wrote:
> Hello all,
>
> I spent some time trying to make overloaded_function support
> polymorphic functions but I had little success :(
>
> 1) I was able to do this (see code below):
>
> overloaded_function<
> void (int)
> , functor<poly, void (generic)>
> > o(f, p);
> o(321);
> o(3.21);
> o("cba");
>
> But I don't like it because I explicitly have to pass the functor type
> poly. I wanted instead to only use generic to tag the template
> parameters in the signature:
>
> overloaded_function<
> void (int)
> , void (generic)
> > o(f, p);
> o(321);
> o(3.21);
> o("cba");
>
> I'd consider this acceptable but I wasn't able to program this because
> I loose p's type (poly) in base so base cannot hold a reference to the
> functor p... Is there a way around this? (Some type-erasure magic
> maybe??)

If the type of o did not involve poly, then you could pass a reference
to o to another translation unit where the definition of poly was not
visible. In that other translation unit you could call it with an
arbitrary type, which would have to instantiate poly::operator() without
sight of its definition. Therefore, I conclude that this is clearly
impossible.

> 2) I don't think this can work with function templates because in
> order to get a pointer to a function template you have to instantiate
> it:
>
> g<char*>
>
> Now I can get the function pointer but it's not polymorphic anymore...

Indeed. We can hope perhaps that some hypothetical future support for
polymorphic lambdas would help here, but even that only reduces it to
the problem of function objects.

> 3) I don't think there is any way to implement
> make_overloaded_function with polymorphic types because I can't
> re-construct the function signature from the functor given that its
> operator contains template parameters... I have no idea on how to work
> around this...

Indeed. Even TTI struggled in this arena:

http://svn.boost.org/svn/boost/sandbox/tti/libs/tti/doc/html/the_type_traits_introspection_library/tti_func_templates.html

> If you have suggestions for 1, 2, or 3), they are welcome. Otherwise,
> it seems that supporting polymorphic functions in overloaded_function
> is not really feasible...

FWIW, I concur. I applaud your efforts, but I think this was always a
long shot.

John


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