Click to See Complete Forum and Search --> : How to fix template ambiguity


eperales
April 9th, 1999, 09:31 AM
Hi all,

I have defined this pair of templates :
template<class T>
inline
CArchive& AFXAPI operator >>(CArchive& ar, T& t)
{
...
}

template<class T>
CArchive& AFXAPI operator >>(CArchive& ar, <B>std::vector<T></B>& v)
{
...
}

eperales
April 9th, 1999, 09:39 AM
Excuse the last message :), as I was saying, I have defined this pair of templates :

template<class T>
inline
CArchive& AFXAPI operator >>(CArchive& ar, T& t)
{
...
}

template<class T>
CArchive& AFXAPI operator >>(CArchive& ar, std::vector<T>& v)
{
...
}

when I compile:

...
std::vector<short> m_vector;
ar >> m_vector;
...

I get the following error :
error C2667: '>>' : none of 2 overload have a best conversion
error C2593: 'operator >>' is ambiguous

why it is ambiguous? it is not the second version a better conversion? and how do I get rid of it?

adios!

Dave Lorde
April 9th, 1999, 11:08 AM
According to Stroustrup (The C++ Programming Language, 3rd Ed., sect. 13.5.2), that should work.

He shows a specialised version of a swap function:

template <class T> void swap(T& x, T& y)
{
T t = x;
x = y;
y = t;
}

then suggests that for vectors, a more efficient method is required:

template <class T> void swap(vector<T>& x, vector<T>& y)
{
x.swap(y); // let vector handle it
}

This doesn't compile under VC++6.0 either... so maybe VC6 just can't handle it.

The STL classes declare specialised swap methods as friend functions inline in the relevant class declaration. Maybe you could try this with a derived vector class...

I don't really know :-(

Dave

eperales
April 9th, 1999, 12:03 PM
Thanks for your input Dave...I also think this is a MS VC "limitation".

>:u(

I will remove one of the templates.

Adiós!