Trying to use templatised fuctions to swap two strings
I can see why people are frowning upon ADL now...
What you see is an effect of Argument Dependent Lookup. If you'd add a print inside your swap
implementation, you'd notice that it is not called for std::string
, only for int
.
std::swap
is preferred over your version, because there exists an explicit specialization for std::basic_string
type. If it didn't exist, call would be ambiguous probably.
For int
, namespace std
is not considered in the lookup process, so your version is the only acceptable.
Also can you tell me that how are stl strings passed as arguements by default i.e are they passed by value or by reference?
Everything in C++ is passed by value, unless you mark it as pass-by-reference explicitly.