First, let me clarify that it is not calling std::swap in your case, I was being hypothetical.
In your case, your swap function is calling itself directly. A non-template definition will be preferred to a template definition when the compiler is trying to figure out which function to call.
For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
// template defined first
template <typename T>
void foo(T& t)
{
std::cout << "template T& foo" << std::endl;
}
void foo(int& t)
{
std::cout << "int& foo" << std::endl;
}
// int& defined first
void bar(int& t)
{
std::cout << "int& bar" << std::endl;
}
template <typename T>
void bar(T& t)
{
std::cout << "template T& bar" << std::endl;
}
int main()
{
int t = 0;
foo(t); // calls int& version, not template version
bar(t); // calls int& version, not template version
}
|
___________________________________________
But besides that, let me focus on what you said:
I still kind of fail to see how an infinite recursion would take place,temp would = a,then a would = b,then b would = temp,but how would that cause infinite recursion all them statements are logically correct?
|
b = temp;
Let's focus on just this:
b is a person.
temp is a person.
How is temp, a person, being assigned to b, a person?
Through the operator= for person. If it's not using the operator=, what operator do you think it's using?
the assignment operator is defined through swap? I thought the assignment operator would be defined in the person class? |
Those are not conflicting statements.
Your assignment operator is defined through swap.
And your assignment operator is defined in your person class.
Both are true, correct.
Your code:
1 2 3 4 5
|
person& operator=(person other){
swap(*this,other);
return *this;
}
|
As you have it now, your assignment operator is dependent on a function called swap. You're not "using namespace std", so there's only 1 option for the compiler to choose. The function you defined that takes in two person references.
Your function,
1 2 3 4
|
void swap(person& one,person &two){
using std::swap;
swap(one,two);
}
|
matches this call, so the compiler will try to call the function you defined, from your operator= function.
Inside
this function, there is a bit of ambiguity for whether or not it calls std::swap vs. your custom swap, but because of the way that templates are deduced, it will prefer calling the existing swap(person&, person&) function, i.e. itself, recursively.
Hope that makes sense, if not I can make an example that's simpler than your OP post.
__________________________________________________________
using std::swap;
is usually done in a
templated function, because you're not sure what type you're actually attempting to use swap on; a built-in type that can just use the standard std::swap, or a custom type that would have its own free swap function associated with it.
_____________________________________________________________
1 2 3 4 5 6
|
void swap(person& one, person &two){
std::swap(one.name, two.name);
std::swap(one.age, two.age);
std::swap(one.len, two.len);
std::swap(one.cstr, two.cstr);
}
|
or
1 2 3 4 5 6
|
void person::swap(person &other){
std::swap(name, two.name);
std::swap(age, two.age);
std::swap(len, two.len);
std::swap(cstr, two.cstr);
}
|
I've made this more confusing than necessary, methinks.