I've been writing a simple program that would swap integers or arrays using templates. I am not sure why, but when I try to run this program it shows me that the function is ambiguous, although function swapping arrays takes three arguments and the one swapping numbers takes 2. Here's the code:
#include <iostream>
using namespace std;
template<typename T>
void swap(T &a, T &b)
{
T dummy = a;
a = b;
b = dummy;
}
template<typename T>
void swap(T a[], T b[], int size)
{
for(int i = 0; i < size;i++)
{
T dum = a[i];
a[i] = b[i];
b[i] = dum;
}
}
int main()
{
int a = 20;
int b = 10;
swap(a, b);
cout << "a: " << a << "\t" << "b: " << b << endl;
int eights[] = {8, 8, 8, 8, 8, 8, 8};
int threes[] = {3, 3, 3, 3, 3, 3, 3};
for(int i = 0; i <7;i++)
{
cout << eights[i] << "\t";
}
for(int i = 0; i <7;i++)
{
cout << threes[i] << "\t";
}
swap(eights, threes, 7);
for(int i = 0; i <7;i++)
{
cout << eights[i] << "\t";
}
for(int i = 0; i <7;i++)
{
cout << threes[i] << "\t";
}
}