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?

2
  • 4
    What's wrong with std::swap? Apr 20, 2020 at 14:55
  • 1
    Nothing wrong with it. I was learning about templatized functions. So i wrote this code just to practice but the output confused me.So i asked. Apr 20, 2020 at 14:57

2 Answers 2

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.

5
  • check out this code here: pastebin.com/E257yw04. My function swap1() is called for strings, but only once. Why is that? Apr 20, 2020 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). Apr 20, 2020 at 15:23
  • Got it!! Thank you so much. Apr 20, 2020 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). Apr 20, 2020 at 15:32
  • Yes I am getting better everyday! Thanks for your reply @Yksisarvinen I will change my username soon. Apr 20, 2020 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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.