About the template function

I just want to transfer the normal insertion sort to be a template function and meet this problem:



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



template <class insertionCode>
void insertion(insertionCode a[], int n)
{
int i, j;
for (i = 1; i < n; ++i)
{
j=i;
while (j>0 && a[j-1]>a[j])
{
swap<insertionCode> (a[j-1], a[j]);
j--;
}
}
}


I get error when I compile it. How can I solve this problem?







It's because your swap function and the standard swap function have the same signature.
Either change the name of your function (my_swap) or put it in your own namespace (my::swap).
Or just delete your swap function as use std::swap.

Also, you do not need the explicit specialization on your call to swap(); the compiler
can deduce the types from the parameters.

Topic archived. No new replies allowed.