Template assignment operator overloading mystery

前端 未结 2 1409
醉话见心
醉话见心 2020-12-06 03:23

I have a simple struct Wrapper, distinguished by two templated assignment operator overloads:

template
struct Wrapper {

  Wra         


        
2条回答
  •  温柔的废话
    2020-12-06 03:26

    Why does assigning d to c not use the const overloaded assignment operator provided?

    The implicitly-declared copy assignment operator, which is declared as follows, is still generated:

    Wrapper& operator=(const Wrapper&);
    

    An operator template does not suppress generation of the implicitly-declared copy assignment operator. Since the argument (a const-qualified Wrapper) is an exact match for the parameter of this operator (const Wrapper&), it is selected during overload resolution.

    The operator template is not selected and there is no ambiguity because--all other things being equal--a nontemplate is a better match during overload resolution than a template.

    Why does assigning b to a not use the default copy assignment operator?

    The argument (a non-const-qualified Wrapper) is a better match for the operator template that takes a Wrapper& than for the implicitly-declared copy assignment operator (which takes a const Wrapper&.

提交回复
热议问题