[C++-sig] Possible overload<> template for class members
David Abrahams
dave at boost-consulting.com
Sat Dec 14 15:24:27 CET 2002
Aleksey Gurtovoy <agurtovoy at meta-comm.com> writes:
> 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?
Yeah, OK.
We could do it with a class template that created a function object,
but I tend to doubt it's worth it.
> 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);
That one seems like the nicest syntax I've seen so far.
--
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