In C++ Templates The Complete Guide in section 5.3 Member Templates it's written:
Note that a template assignment operator doesn't replace the default assignment operator. For assignments of stacks of the same type, the default assignment operator is still called.
Is this correct, because when I ran below code:
#include<iostream>
using namespace std;
template<typename T>
class Pair
{
public:
T pair1,pair2;
Pair(T i,T j):pair1(i),pair2(j){}
template<typename T1>Pair<T>& operator=(Pair<T1>&);
};
template<typename T>
template<typename T1>
Pair<T>& Pair<T>::operator=(Pair<T1>& temp)
{
this->pair1 =temp.pair1*10;//At this point
this->pair2=temp.pair2;
return *this;
}
int main()
{
Pair<int>P1(10,20);
Pair<int>P2(1,2);
P2=P1;
cout<<P2.pair1<<' '<<P2.pair2<<endl;
return 1;
}
I got answer 100 20.
It didn't give the default assignment answer.
Is that a typing mistake in C++ Templates the Complete Guide?
C++ Templates: The Complete Guide By David Vandevoorde, Nicolai M. Josuttis
Publisher : Addison Wesley
Pub Date : November 12, 2002 Pages : 552
If a class definition does not declare a parameterless constructor, a copy constructor, a copy assignment operator, or a destructor, the compiler will implicitly declare them. These are called default operators. A C-like struct has these default operators.
The assignment operator must be overloaded as a member function. This will call f1. operator=(f1), and under the simplistic implementation above, all of the members will be assigned to themselves.
If no user-defined copy assignment operators are provided for a class type (struct, class, or union), the compiler will always declare one as an inline public member of the class.
Assignment Operators Overloading in C++You can overload the assignment operator (=) just as you can other operators and it can be used to create an object just like the copy constructor. Following example explains how an assignment operator can be overloaded.
The copy assignment operator is indeed implicitly declared and considered by overload resolution.
A user-declared copy assignment operator
X::operator=
is a non-static non-template member function of classX
[..].
If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. [..] The implicitly-declared copy assignment operator for a classX
will have the formX& X::operator=(const X&)
if
- each direct base class
B
ofX
has a copy assignment operator whose parameter is of typeconst B&
,const volatile B&
orB
, and- for all the non-static data members of
X
that are of a class typeM
(or array thereof), each such class type has a copy assignment operator whose parameter is of typeconst M&
,const volatile M&
orM
.Otherwise, [..]
As you can see the implicitly-declared copy assignment operator for Pair<int>
has one parameter of type Pair<int> const&
- note the const
in particular! Overload resolution favours non-const
references over const
ones if both can be bound to the argument, [over.ics.rank]/3:
Two implicit conversion sequences of the same form are indistinguishable conversion sequences unless one of the following rules applies:
— Standard conversion sequence
S1
is a better conversion sequence than standard conversion sequenceS2
if
- [..]
S1
andS2
are reference bindings (8.5.3), and the types to which the references refer are the same type except for top-level cv-qualifiers, and the type to which the reference initialized byS2
refers is more cv-qualified than the type to which the reference initialized byS1
refers.
The specialization of the template lacks a const
in the reference parameter, thus it's a better match and is selected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With