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

David Abrahams dave at boost-consulting.com
Fri Dec 13 13:56:02 CET 2002


"Joel de Guzman" <djowel at gmx.co.uk> writes:

> ----- Original Message ----- 
> From: "Joel de Guzman" <djowel at gmx.co.uk>
>
>> ----- Original Message ----- 
>> From: "Aleksey Gurtovoy" <agurtovoy at meta-comm.com>
>> 
>> 
>> > Ralf W. Grosse-Kunstleve wrote:
>> > > --- William Trenker <wtrenker at hotmail.com> wrote:
>> > > > .def("show", overload<void,void>(), &Window::show)
>> > > > .def("show", overload<void,const Window*>(), &Window.show)
>> > > 
>> > > That looks just like my old "plan A" :-)
>> > > 
>> > > >   Is something like this even possible?
>> > > 
>> > > David kept claiming the answer is no, because in C++ you cannot take
>> > > the "address" of a family over overloaded functions. If Window.show 
>> > > has overloads the compiler has no way of knowing which one you mean 
>> > > if you just type &Window.show.

I didn't claim the answer was no for this case.  Only for the case
where you want to generate all of the overloads at once by just
mentioning the function name.

>> > 
>> > Hmm, seems entirely possible to me:
>> > 
>> [snip]
>> > 
>> > Am I missing something?
>> 
>> Cool Aleksey, cool! I think your scheme will work, AFAICT.
>> Might need some workarounds on VC6 and CW7.2.
>> Compiles on g++2.95, g++3.1, Comeau, intel and VC7.
>> 
>> However, it still can't handle default arguments automatically :-(
>> For instance:
>> 
>>     void show(int = 45, bool* = 0, char 'x') {}
>> 
>> I still see no way to def this in one fell swoop. You still need
>> three defs.
>> 
>> ... Or am I missing something?
>
> Furthermore, you can't def overloads with common arguments
> automatically in one shot:
>
>     void show() {}
>     void show(int) {}
>     void show(int, bool*) {}
>     void show(int, bool*, char) {}
>
> Oh well....
>
> Anyway, it seems better than having to cast the function pointer
> for overloads.

Why?

It seems to cost almost exactly the same, syntactically, though it is
a bit safer.

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?  It seems like it would be really useful as a
generic component.

-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list