Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C++] ambiguous call to overloaded function

Started by jorgander Jul 30, 2010 at 6:42 PM 1 replies 2k views
Original Post
jorgander
jorgander
I have these two string conversion functions:
template <typename T> const T * UTIL::Convert(const T *, const size_t, size_t &);template <typename T, typename U> const T * UTIL::Convert(const U *, const size_t, size_t &);

The first simply returns its parameter, since they are the same type and no conversion is necessary. The second performs a conversion using wcstombs, mbstowcs, or some other method.

This line of code
const UTIL::Char * ptrText = UTIL::Convert<UTIL::Char>(p_ptrText, p_intLength, uintLength);

Produces this error
1>c:\development\assware\common\src\gui\factory.cpp(220) : error C2668: 'UTIL::Convert' : ambiguous call to overloaded function1>        c:\development\util\include\util\impl\traits.h(47): could be 'const T *UTIL::Convert<UTIL::Char,XML_Char>(const U *,const size_t,size_t &)'1>        with1>        [1>            T=UTIL::Char,1>            U=XML_Char1>        ]1>        c:\development\util\include\util\impl\traits.h(14): or       'const T *UTIL::Convert<UTIL::Char>(const T *,const size_t,size_t &)'1>        with1>        [1>            T=UTIL::Char1>        ]1>        while trying to match the argument list '(const XML_Char *, int, size_t)'

I'm wondering how to achieve the effect of automatically calling the correct Covert method using templates. That is to say, the compiler should know when the types are the same and call the first method.
Shinkage
Shinkage
template<typename T, typename U>struct convert_impl{   static const T* apply(const U*, const size_t, size_t&);};template<typename T>struct convert_impl<T, T>{   static const T* apply(const T*, const size_t, size_T&);};template<typename T, typename U>const T* convert(const U *p1, const size_t p2, size_t &p3){   return convert_impl<T, U>::apply(p1, p2, p3);}


Something like that perhaps?
jorgander
jorgander
That compiles alright, thanks for the suggestion. I'll have to test and make sure the correct version is called.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.