Alright so heres the program and works absolutely right
#include <iostream>
using namespace std;
template <typename T>
void Swap(T &a , T &b);
int main(){
int i = 10;
int j = 20;
cout<<"i, j = " << i <<" , " <<j<<endl;
Swap(i,j);
cout<<"i, j = " << i <<" , " <<j<<endl;
}
template <typename T>
void Swap(T &a , T &b){
T temp;
temp = a ;
a = b;
b= temp;
}
but when I change the function's name from Swap to swap it generates an error saying
error: call of overloaded 'swap(int&, int&)' is ambiguous| note: candidates are: void swap(T&, T&) [with T = int]| ||=== Build finished: 1 errors, 0 warnings ===|
what happened is it a rule to start functions using templates to start with a capital letter ?
This is because there already exists a function called swap
. It is actually under the std
namespace, but because you have a using namespace std
line, it exists without the std::
prefix.
As you can see, using the using namespace std
isn't always a good option because of possible name collisions, as in this example. In general one should prefer not to use the using
directive unless there's a real reason for this - namespaces exist for a reason - to prevent name collisions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With