I am using the following sample program in C++
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
namespace mine{
template<class T>
inline void swap(T &a, T &b){
char c= a; //This should not have compiled
a=b;
b=c;
}
}
int main(){
int a,b;
cout<< "Enter two values: ";
cin>>a>>b;
mine::swap(a,b); //type variable T is instantiated as in
cout << a <<' '<<b << endl;
}
I am expecting the compiler to throw an error in the swap function, because c is declared as a char, but assigned a variable of generic type variable T. Not only that, when invoking swap, T is instantiated as int. However, not only is g++ not giving any error, the program works perfectly. Why is this the case?