Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template assignment operator doesn't replace the default assignment operator

Tags:

c++

templates

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

like image 900
InQusitive Avatar asked Dec 28 '14 17:12

InQusitive


People also ask

Is assignment operator default in c++?

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.

Is assignment operator overloaded by default?

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.

What happens if you don't define a copy assignment operator?

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.

Is assignment operator overloaded by default in C++?

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.


1 Answers

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 class X [..].

If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. [..] The implicitly-declared copy assignment operator for a class X will have the form

X& X::operator=(const X&)

if

  • each direct base class B of X has a copy assignment operator whose parameter is of type const B&, const volatile B& or B, and
  • for all the non-static data members of X that are of a class type M (or array thereof), each such class type has a copy assignment operator whose parameter is of type const M&, const volatile M& or M.

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 sequence S2 if

  • [..]
  • S1 and S2 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 by S2 refers is more cv-qualified than the type to which the reference initialized by S1 refers.

The specialization of the template lacks a const in the reference parameter, thus it's a better match and is selected.

like image 71
Columbo Avatar answered Sep 19 '22 15:09

Columbo