C++ Generic Problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace 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;
}

Why i couldn't run successfully ?
Define "couldn't run successfully".

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".
O , THX a lot!
I am a noob of C++ , i dont know the swap function has been integrating with std
You can write simply

::swap(n,k);
Topic archived. No new replies allowed.