1
int main() {
    // Complete the program
    string a,b;
    getline(cin,a);
    getline(cin,b);
    cout<<a.size()<<" ";
    cout<<b.size();
    string c=a+b;
    cout<<endl<<c;

    swap(a[0],b[0]);
    cout<<endl<<a<<" "<<b;
    return 0;
}
void swap(string s1,string s2){
    string temp=s1;
    s1=s2;
    s2=temp;
}

Well the target is to swap the first element of both strings, but I created a general function for that and even got it right. But, unexpectedly, I didn't use pass by reference or pointer! Even then, the changes are permanent when I try to output a and b in the end!

Logically it shouldn't work but it is working. Is it something to do with the strings?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 2
    Note: [std::swap](https://en.cppreference.com/w/cpp/algorithm/swap) exists. – Jesper Juhl May 02 '20 at 18:05
  • Post all of your code, including the header inclusions, not snippets. There is a `std::swap` function, and more than likely, that function is the one being called. – PaulMcKenzie May 02 '20 at 18:06
  • Could you tell us which input for a and b you used? I agree, this shouldn't be working... – Anonymous1847 May 02 '20 at 18:06
  • 2
    Did you know that I'm telepathic? I happen to know that you have `using namespace std;` at the beginning of your code, even though you failed to show it. I should go into fortune-telling... – Sam Varshavchik May 02 '20 at 18:06
  • Ah, nevermind. Yes, it's to do with std::swap name ambiguity. One of you should make an answer. – Anonymous1847 May 02 '20 at 18:07
  • 1
    `a[0]` and `b[0]` are individual `char` references. It's not calling your function which takes strings. – Blastfurnace May 02 '20 at 18:08
  • `std::string` does not have a constructor that takes a single `char` by itself, so `swap(a[0],b[0])` will NEVER be able to call `void swap(string s1,string s2)`, no matter what you do with namespaces. – Remy Lebeau May 02 '20 at 20:35

1 Answers1

8

This is almost certainly due to the fact that, somewhere in code that you have not shown us, you have this line (or something very similar):

using namespace std;

With this line included, then that very namespace std defines a function as follows:

void swap(_Ty& _Left, _Ty& _Right);

Where the _Ty template is replaced with char in your swap(a[0],b[0]); call.

Add a simple cout << "My Swap" << endl; line to your swap function, and you'll see it's not being called.

Highly recommended reading: Why is "using namespace std;" considered bad practice?.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • thanks a lot i got it! but even though i am defining swap in my own way; the function of the namespace is being used! – Gaganrajdeep Singh May 02 '20 at 18:25
  • Yes - the compiler will find the 'best match' to your call, which is *not* the one you provided. Replace `using namespace std;` with lines like `using std::cout; using std::cin; using std::endl; using std::string;` and the compiler will then give you an error that the fuction call is wrong. – Adrian Mole May 02 '20 at 18:27
  • if i remove the namespace std; it should actually work because then the best match will be the function defined by me; isnt it? – Gaganrajdeep Singh May 02 '20 at 21:10
  • @GaganrajdeepSingh No, because your function takes strings while you give it single characters. – eesiraed May 02 '20 at 21:38
  • @BessieTheCow isnt that allowed? i may have seen problems where single char values works fine with strings! – Gaganrajdeep Singh May 02 '20 at 22:19
  • 1
    @GaganrajdeepSingh There is no conversion from a single character to an `std::string`, although some overloaded operators for `std::string` accept single characters. – eesiraed May 02 '20 at 22:25