I have a simple Wrapper structure, featuring two overloaded assignment pattern operations:
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; } };
Then I declare a and b:
Wrapper<float> a, b; a = b;
assignment b to a will use the overload of the non-constant template assignment operator on top and the number "2" will be displayed.
What puzzles me: if I declare c and d ,
Wrapper<float> c; const Wrapper<float> d; c = d;
and assign d c , none of the two overloads of the destination operator is used, and the output is not output; therefore, the default copy destination statement is called. Why is the overloaded assignment operator const not used when assigning d to c ? Or instead, why assigning b to a does not use the default copy operator?
user2023370
source share