I am currently using C++ 17, and I wanted to simplify my program down. However, I am thrown error:
more than one instance of overloaded function \"swap\" matches the argument list: -- function template \"void swap(T &a, T &b)\" -- function template \"std::enable_if<std::__and_<std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp>>::value, void>::type std::swap(_Tp &__a, _Tp &__b)\" -- argument types are: (int, int)",
This is the code I wrote:
#include <iostream>
#include <string>
using namesapce std;
template <typename T>
void swap(T &a, T &b) {
T temp = a; //Temporary copy to reuse
a = b;
b = temp;
}
int main() {
int x, y;
cin >> x >> y;
cout << "X: " << x << "\t Y: " << y << endl; //Tester
swap(x, y) //Where the error happened
cout << "New X: " << x << "\t New Y: " << y << endl; //Tester
string name1 = "FOO";
string name2 = "BAR";
swap(name1, name2);
cout << name1 << "\t" << name2 << endl;
return 0;
}
Interestingly, the code doesn't throw any error when I call the next swap(). Is swap incompatible with integers or is there some catch?
Thanks for your help, in advance! Edit: My code to swap doesn't even make sense. SORRY!