Why can I swap string with double parameters?

Hello,
I don't really understand why code below is working. What I don't understand is why can I swap strings using swap() with double parameters. How is it possible?
Thanks for your answers.

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
28
29
30
#include<iostream>
#include<vector>
#include<string>
using namespace std;

void swap(double& a, double& b) {
	int temp;
	temp = a;
	a = b;
	b = temp;
}

//reverse vector 
void reverse(vector<string>& v) {
	for (int i = 0; i < v.size() / 2; ++i)
		swap(v[i], v[v.size() - i - 1]);
	for (int i = 0; i < v.size(); ++i)
		cout << v[i] << " ";
}

int main()
{
	vector<string>names = { "something1","something2","something3","something4","something5" };

	cout << "\nReverse using reverse(): ";
	reverse(names);

	cout << '\n';
	return 0;
}
This is one of the surprises that using namespace std; can lead to. The function that you are calling is actually std::swap<std::string>.

http://www.cplusplus.com/reference/algorithm/swap/
Last edited on
Oh, that actually make a lot of sense now. Thanks for your answer.
Your program will call std::swap which has a built-in specialisation for std::string

http://www.cplusplus.com/reference/string/string/swap-free/

Try renaming your swap() function to myswap() (and the place where you call the function too) and see whether it still works.
> This is one of the surprises that using namespace std; can lead to.

This is one of the pleasant surprises that argument-dependent lookup can lead to.

Note that there is no using namespace std; directive in the following code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
	std::string a = "argument dependent look up (ADL) "
	                "is also known as koening look up\n" ;
	std::string b = "see: http://en.cppreference.com/w/cpp/language/adl " ;

	swap(a,b) ; // ADL finds std::swap
	a == b ; // ADL finds std::operator==
	std::cout << b+a ; // ADL finds std::operator+, std::operator<<
	a = "1234567" ; stoi(a) ; // ADL finds std:stoi
}
Your program will call std::swap which has a built-in specialisation for std::string

I believe this is entirely due to ADL as JLBorges says. Besides, the reference section doesn't indicate that there is a specialization for swapping strings.

[ Edit - the page that Chervil linked for string does describe the specialization. ]
Last edited on
Well, the reference page I linked describes it as "an overload of the generic algorithm swap" which is a better description than the way I put it.
The function that is called for swapping two std::string objects is a specialisation of std::swap()
(Though, it is not an explicit specialisation.)

The std::swap defined in the header <string> is a function template
http://en.cppreference.com/w/cpp/string/basic_string/swap2 , and
A function instantiated from a function template is called a function template specialization;
so is an explicit specialization of a function template. - IS

Topic archived. No new replies allowed.