ambiguous call to overloaded function

  • Follow


{ This article is multi-posted to [comp.lang.c++]. -mod }

I want to overload function to swap two others types, for example,
type<T>, T* and so on,
but I can't compile the following code at VC 6.0.

The compiler says :
error C2667: 'swap' : none of 2 overload have a best conversion
error C2668: 'swap' : ambiguous call to overloaded function
---------------------------------------------------------------------------�----------------------------------
template < typename T >
void swap(T &lhs, T &rhs)
{
         // do some thing



}


template < typename T >
void swap(T *lhs, T *rhs)
{
         // do some thing


}


template < typename T >
struct type{};

template < typename T >
void swap(type<T> &lhs, type<T> &rhs)
{
         // do some thing


}


int main()
{
         int *p1,*p2;
         type<int> t1, t2;
         swap(t1, t2);
         swap(p1, p2);



-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Ruki 6/30/2008 8:01:52 AM

On Jun 30, 5:01 pm, Ruki <war...@gmail.com> wrote:
> I want to overload function to swap two others types, for example,
> type<T>, T* and so on,
> but I can't compile the following code at VC 6.0.
>
> The compiler says :
> error C2667: 'swap' : none of 2 overload have a best conversion
> error C2668: 'swap' : ambiguous call to overloaded function
> ---------------------------------------------------------------------------�----------------------------------
> template < typename T >
> void swap(T &lhs, T &rhs)
> {
>          // do some thing
>
> }
>
> template < typename T >
> void swap(T *lhs, T *rhs)
> {
>          // do some thing
>
> }
>
> template < typename T >
> struct type{};
>
> template < typename T >
> void swap(type<T> &lhs, type<T> &rhs)
> {
>          // do some thing
>
> }
>
> int main()
> {
>          int *p1,*p2;
>          type<int> t1, t2;
>          swap(t1, t2);
>          swap(p1, p2);
>

Hello,

Please always include minimal compilable code.

Adding '#include <algorithm>' to the top of the above code, and
closing the 'main' block, the code compiles fine with both Microsoft
Visual C++ 2005 and Comeau C++ online compiler. You may want to try
updating your compiler.

Regards.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply alasham 7/1/2008 6:44:41 PM


On Jun 30, 8:01 pm, Ruki <war...@gmail.com> wrote:
> { This article is multi-posted to [comp.lang.c++]. -mod }
>
> I want to overload function to swap two others types, for example,
> type<T>, T* and so on,
> but I can't compile the following code at VC 6.0.
>
> The compiler says :
> error C2667: 'swap' : none of 2 overload have a best conversion
> error C2668: 'swap' : ambiguous call to overloaded function
> ---------------------------------------------------------------------------��----------------------------------
> template < typename T >
> void swap(T &lhs, T &rhs)
> {
>          // do some thing
>
> }
>
> template < typename T >
> void swap(T *lhs, T *rhs)
> {
>          // do some thing
>
> }
>
> template < typename T >
> struct type{};
>
> template < typename T >
> void swap(type<T> &lhs, type<T> &rhs)
> {
>          // do some thing
>
> }
>
> int main()
> {
>          int *p1,*p2;
>          type<int> t1, t2;
>          swap(t1, t2);
>          swap(p1, p2);
>
> --

What you have tried to achieve is something like partial template
specialization of functions - which C++ does not allow. You could
perhaps use a function object with different overloads of the function-
call operator. Something like:

template < typename T >
struct type{};


template < typename T >
struct my_swap
{
	void operator () (T &lhs, T &rhs) {
         // do some thing
         cout << "Regular overload" << endl;
	}

	void operator () (T *lhs, T *rhs) {
         // do some thing
         cout << "Pointer overload" << endl;
	}

	void operator () (type<T> &lhs, type<T> &rhs) {
         cout << "type<...> overload" << endl;
	}
};


int main()
{
         int i1 = 4, i2 = 6;
         int *p1 = &i1, *p2 = &i2;
         type<int> t1, t2;
         my_swap<int>()(i1, i2);
         my_swap<int>()(t1, t2);
         my_swap<int>()(p1, p2);
}

- Arindam


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Arindam 7/1/2008 6:52:27 PM

Arindam <arindam.muker...@gmail.com> wrote:
>
> template < typename T >
> struct type{};
>
> template < typename T >
> struct my_swap
> {
>         void operator () (T &lhs, T &rhs) {
>          // do some thing
>          cout << "Regular overload" << endl;
>         }
>
>         void operator () (T *lhs, T *rhs) {
>          // do some thing
>          cout << "Pointer overload" << endl;
>         }
>
>         void operator () (type<T> &lhs, type<T> &rhs) {
>          cout << "type<...> overload" << endl;
>         }
>
> };

Thanks, It's a good idea. I slightly modify this code.
Like this:

template < typename T >
struct type{};

template < typename T >
struct swap_impl
{
	static void swap(T &lhs, T &rhs)
	{
		// do some thing
	}
	static void swap(type<T> &lhs, type<T> &rhs)
	{
		// do some thing
	}
};


template < typename T >
void swap(T &lhs, T &rhs)
{
	swap_impl<T>::swap(lhs, rhs);
}

int main()
{
	type<int> t1, t2;
	swap(t1, t2);


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Ruki 7/5/2008 3:58:10 AM

3 Replies
433 Views

(page loaded in 0.08 seconds)

Similiar Articles:













7/25/2012 10:20:19 AM


Reply: