|
Boost : |
From: Aleksey Gurtovoy (alexy_at_[hidden])
Date: 2002-02-23 10:47:14
David Abrahams wrote:
> > But you still end up with the following ambiguity on MSVC:
> >
> > template<typename T>
> > struct A {
> > };
> >
> > template<typename T>
> > void foo( T const& ) {}
> >
> > template<typename T>
> > void foo( A<T> const& ) {}
> >
> > int main(int, char*[])
> > {
> > A<int> a_int;
> >
> > foo( a_int );
> >
> > return 0;
> > }
> >
> > error C2667: 'foo' : none of 2 overload have a best conversion
> >
> > Anyone knows a fix?
>
> It depends whether 'A' is supposed to represent a
> user-defined type or a template supplied by the library. If the
> latter, you can intrude on foo and detect that T is an A<T> using
> the sizeof() trick, then dispatch as appropriate. Otherwise, I'm
> afraid there's no way.
#include <iostream>
template< typename T > struct A {};
namespace aux {
template< typename T >
struct foo_impl
{
template< typename U >
static void foo(A<U> const&, long)
{
std::cout << "foo(A<U> const&)\n";
}
static void foo(T const&, int)
{
std::cout << "foo(T const&)\n";
}
};
}
template< typename T >
void foo(T const& t)
{
aux::foo_impl<T>::foo(t, 1L);
}
int main()
{
foo(int());
foo(A<int>());
return 0;
}
That's more, I belive that the technique allows you to simulate partial
ordering of any number of overloaded function templates, not just 2 of them.
Aleksey
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk