c++ - Template assignment operator overloading mystery -


i have simple struct wrapper, distinguished 2 templated assignment operator overloads:

template<typename t> struct wrapper {    wrapper() {}    template <typename u>   wrapper &operator=(const wrapper<u> &rhs) {     cout << "1" << endl;     return *this;   }   template <typename u>   wrapper &operator=(wrapper<u> &rhs) {     cout << "2" << endl;     return *this;   } }; 

i declare , b:

wrapper<float> a, b; = b; 

assigning b a use non-const templated assignment operator overload above, , number "2" displayed.

what puzzles me this: if declare c , d,

wrapper<float> c; const wrapper<float> d; c = d; 

and assign d c, neither of 2 assignment operator overloads used, , no output displayed; default copy assignment operator invoked. why assigning d c not use const overloaded assignment operator provided? or instead, why assigning b a not use default copy assignment operator?

why assigning d c not use const overloaded assignment operator provided?

the implicitly-declared copy assignment operator, declared follows, still generated:

wrapper& operator=(const wrapper&); 

an operator template not suppress generation of implicitly-declared copy assignment operator. since argument (a const-qualified wrapper) exact match parameter of operator (const wrapper&), selected during overload resolution.

the operator template not selected , there no ambiguity because--all other things being equal--a nontemplate better match during overload resolution template.

why assigning b a not use default copy assignment operator?

the argument (a non-const-qualified wrapper) better match operator template takes wrapper<u>& implicitly-declared copy assignment operator (which takes const wrapper<u>&.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -