#include <iostream>
usingnamespace std;
template <typename T>
void swap(T & a , T & b);
template <typename T>
void swap(T & a , T & b)
{
T temp = a;
a = b;
b = temp;
}
int main()
{
int n = 20;
int k = 40;
swap(n,k);
cout << "n : " << n << ", k : " << k << "\n";
return 0;
}
On my test system, this program does not compile because the call to swap() is ambiguous: the standard C++ function std::swap() and the function you wrote are equally good:
"test.cc", line 21.9: 1540-0219 (S) The call to "swap" has no best match.
"test.cc", line 21.14: 1540-1228 (I) Argument number 1 is an lvalue of type "int".
"test.cc", line 21.16: 1540-1228 (I) Argument number 2 is an lvalue of type "int".
"test.cc", line 9.6: 1540-1202 (I) No candidate is better than "swap<int>(int &, int &)".
"test.cc", line 21.14: 1540-1231 (I) The conversion from argument number 1 to "int &" uses "the identity conversion".
"test.cc", line 21.16: 1540-1231 (I) The conversion from argument number 2 to "int &" uses "the identity conversion".
"/bb/util/version11-042012/usr/vacpp/include/xutility", line 756.14: 1540-1202 (I) No candidate is better than "std::swap<int>(int &, int &)".
"test.cc", line 21.14: 1540-1231 (I) The conversion from argument number 1 to "int &" uses "the identity conversion".
"test.cc", line 21.16: 1540-1231 (I) The conversion from argument number 2 to "int &" uses "the identity conversion".