-1

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";
        
    }
    
    
    


}

2 Answers2

0

As mentioned in the comment you should do some changes to remove the ambiguity between the std::swap and yours. remove using namespace std; and use std:: before cout:

#include <iostream>

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);
    std::cout << "a: " << a << "\t" << "b: " << b << std::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++)
    {
        std::cout << eights[i] << "\t";
        
    }
    for(int i = 0; i <7;i++)
    {
        std::cout << threes[i] << "\t";
        
    }
    
    swap(eights, threes, 7);
    for(int i = 0; i <7;i++)
    {
        std::cout << eights[i] << "\t";
        
    }
    for(int i = 0; i <7;i++)
    {
        std::cout << threes[i] << "\t";
        
    }
}
G. Emadi
  • 230
  • 1
  • 8
0

The reason is that, in your implementation (i.e. compiler and standard library) <iostream> brings in the standard algorithm std::swap() (which is declared in <algorithm>). That behaviour is not required by the standard, but also not forbidden by it.

By using using namespace std, candidates for resolving the call swap(a, b) are found both in namespace std, and in your templated swap(). Both candidates can accept two arguments, and both have arguments of the same type, hence the ambiguity.

The fix is to remove your templated swap() that accepts two arguments. To ensure portability of your code, add #include <algorithm> (i.e. don't rely on std::swap() being available because of #include <iostream>, since that is not actually guaranteed).

Although not strictly necessary in your case, it is often considered advisable to avoid using using namespace std in your code. If you really want to write your own templated function named swap(), removing using namespace std will help ensure your function does not introduce ambiguity with std::swap().

Incidentally, if you are using C++11 or later, your three-argument swap() is also not necessary, since <algorithm> provides a std::swap() overload that can swap two raw arrays with any type of element (if the size is known at compile time).

Peter
  • 35,646
  • 4
  • 32
  • 74