Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to use templatised fuctions to swap two strings

#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?

like image 253
gettingBetterprogrammer Avatar asked Apr 20 '20 14:04

gettingBetterprogrammer


People also ask

How do you swap two strings in CPP?

Consider two strings s1 and s2 , we want to exchange the values of these two string objects. Its syntax would be : s1. swap(s2)

How do you swap two numbers in C++ using functions?

swap() function in C++ The swap() function is used to swap two numbers. By using this function, you do not need any third variable to swap two numbers. Here is the syntax of swap() in C++ language, void swap(int variable_name1, int variable_name2);

What is the syntax of SWAP ()?

Syntax. Following is the declaration of swap() method: public static void swap(List<?> list, int i, int j)

What is member function template in C++?

Member function templates are function templates that are members of a class or class template. Member functions can be function templates in several contexts. All functions of class templates are generic but aren't referred to as member templates or member function templates.


1 Answers

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.

like image 115
Yksisarvinen Avatar answered Nov 15 '22 17:11

Yksisarvinen