[C++-sig] Possible overload<> template for class members

Aleksey Gurtovoy agurtovoy at meta-comm.com
Sat Dec 14 10:46:21 CET 2002


David Abrahams wrote:
> As soon as we have implicit_cast, though, which I was planning to
> check in last week, we can write:
> 
>    .def("show", implicit_cast<void(Window::*)(void)>(&Window::show))
>    .def("show", implicit_cast<void(Window::*)(const 
> Window*)>(&Window::show))
> 
> Okay, so for member functions you end up writing Window:: twice.
> That's a bit of a pain.  I don't want to get this all wound up in the
> "def" machinery, which is complicated enough already.
> 
> Now, of course we could make a version of implicit_cast called
> "overload"... and it seems to me that since you can't overload on
> return type, it should be possible to omit that.
> 
> So how about:
> 
>    .def("show", select_overload<void>(&Window::show))
>    .def("show", select_overload<const Window*>(&Window::show))
> 
> Can't we implement that?  

Not in that exact form. Consider, for instance, this:

    template< typename T > struct identity
    {
        typedef T type;
    };

    template< typename A0, typename R >
    void
    overload_cast(R (*f)(typename identity<A0>::type));

    template< typename A0, typename A1, typename R >
    void 
    overload_cast( R (*f)( 
          typename identity<A0>::type
        , typename identity<A1>::type
        ) );

    bool show(int);
    void show(int, bool);

    int main()
    {
        overload_cast<int,bool>(&show); // ambiguous!
    }

Nasty, huh?

This one is always possible, of course:

    void show();
    bool show(int);
    void show(int, bool*);
    void show(int, bool);

    struct window
    {
        void show();
        bool show(int);
        void show(int, bool*);
        void show(int, bool);
    };

    int main()
    {
        overload(args<>(), &show);
        overload(args<int>(), &show);
        overload(args<int,bool*>(), &show);
        overload(args<int,bool>(), &show);

        overload(args<>(), &window::show);
        overload(args<int>(), &window::show);
        overload(args<int,bool*>(), &window::show);
        overload(args<int,bool>(), &window::show);
    }

Or we can move the 'args' into function templates' template parameter list:

        overload_cast< args<int,bool> >(&show);

> It seems like it would be really useful as a generic component.

I am not sure about the "really" part, but it might be :).

Aleksey




More information about the Cplusplus-sig mailing list