9
#include<iostream>
#include<string>

template <typename T>
void swap(T a , T b)
{
  T temp = a;
  a = b;
  b = temp;
}

template <typename T1>
void swap1(T1 a , T1 b)
{
  T1 temp = a;
  a = b;
  b = temp;
}

int main()
{
  int a = 10 , b = 20;
  std::string first = "hi" , last = "Bye";

  swap(a,b);
  swap(first, last);   

  std::cout<<"a = "<<a<<" b = "<<b<<std::endl;
  std::cout<<"first = "<<first<<" last = "<<last<<std::endl;    

  int c = 50 , d = 100;
  std::string name = "abc" , surname = "def";

  swap1(c,d);
  swap1(name,surname);

  std::cout<<"c = "<<c<<" d = "<<d<<std::endl;
  std::cout<<"name = "<<name<<" surname = "<<surname<<std::endl;    

  swap(c,d);
  swap(name,surname);

  std::cout<<"c = "<<c<<" d = "<<d<<std::endl;
  std::cout<<"name = "<<name<<" surname = "<<surname<<std::endl;    

  return 0;
}

**Output**
a = 10 b = 20
first = Bye last = hi
c = 50 d = 100
name = abc surname = def
c = 50 d = 100
name = def surname = abc

Both swap() and swap1() basically have the same function-definitions then why only swap() actually swaps the strings, while swap1() does not?

Also can you tell me that how are stl strings passed as arguments by default i.e are they passed by value or by reference?

Evg
  • 25,259
  • 5
  • 41
  • 83

2 Answers2

9

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.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • check out this code here: https://pastebin.com/E257yw04. My function swap1() is called for strings, but only once. Why is that? – gettingBetterprogrammer Apr 20 '20 at 15:18
  • @dumb_programmer You have two calls to swap1 in your code (one for `int` and one for `std::string`), and so two calls are printed. There are two calls to `swap` with `std::string`, and those use `std::swap` (no print). – Yksisarvinen Apr 20 '20 at 15:23
  • Got it!! Thank you so much. – gettingBetterprogrammer Apr 20 '20 at 15:26
  • @dumb_programmer Side note: please don't call yourself dumb. Imposter syndrome is common in this job, but real value of programmer is not "how much he knows" but rather "how much is he willing to learn". You didn't understand something, so you reached out and asked - this is the best possible action to do (after googling first of course). – Yksisarvinen Apr 20 '20 at 15:32
  • Yes I am getting better everyday! Thanks for your reply @Yksisarvinen I will change my username soon. – gettingBetterprogrammer Apr 20 '20 at 16:01
0

You are passing parameters by value. You need to pass them by reference:

template <typename T> void myswap(T& a , T& b);

Or - more generally - by global(rvalue) reference:

template <typename T> void myswap(T&& a , T&& b);
Red.Wave
  • 2,790
  • 11
  • 17