CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 1999
    Location
    Monterrey, Mexico
    Posts
    21

    How to fix template ambiguity

    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)
    {
    ...
    }



  2. #2
    Join Date
    Apr 1999
    Location
    Monterrey, Mexico
    Posts
    21

    Re: How to fix template ambiguity

    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!


  3. #3
    Join Date
    Apr 1999
    Posts
    383

    Re: How to fix template ambiguity

    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



  4. #4
    Join Date
    Apr 1999
    Location
    Monterrey, Mexico
    Posts
    21

    Re: How to fix template ambiguity (THANKS)

    Thanks for your input Dave...I also think this is a MS VC "limitation".

    >:u(

    I will remove one of the templates.

    Adiós!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured