Special member functions (copy/move assignment) - C++11, 22 of n

copy/move assignment

  • A user-declared copy assignment operator X::operator= isnon-static non-template member function of class X with exactly one parameter of type X, X&, const X&, volatile X& or const volatile X&. This means that a template assignment operator or an assignment operator taking an rvalue reference parameter isnever a copy assignment operator, the presence of such an assignment operator does not suppress the implicit declaration of a copy assignment operator. Such assignment operators participate in overload resolution with other assignment operators, including copy assignment operators, and, if selected, will be used to assign an object.
  • If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy assignment operator is defined as deleted; otherwise, it is defined as defaulted. The latter case is deprecated if the class has a user-declared copy constructor or a user-declared destructor. 
  • 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, the implicitly-declared copy assignment operator will have the form X& X::operator=(X&)
  • A user-declared move assignment operator X::operator= is non-static non-template member function of class X with exactly one parameter of typeX&&, const X&&, volatile X&&, or const volatile X&&.
  • If the definition of a class X does not explicitly declare a move assignment operator, one will be implicitly declared as defaulted if and only if
    • X does not have a user-declared copy constructor
    • X does not have a user-declared move constructor
    • X does not have a user-declared copy assignment operator, and
    • X does not have a user-declared destructor
  • A defaulted copy/move assignment operator for class X is defined as deleted if X has:
    • a variant member with a non-trivial corresponding assignment operator and X is a union-like class, or
    • a non-static data member of const non-class type (or array thereof), or
    • a non-static data member of reference type, or
    • a non-static data member of class type M (or array thereof) that cannot be copied/moved because overload resolution, as applied to M’s corresponding assignment operator, results in an ambiguity or a function that is deleted or inaccessible from the defaulted assignment operator, or
    • a direct or virtual base class B that cannot be copied/moved because overload resolution, as applied to B’s corresponding assignment operator, results in an ambiguity or a function that is deleted or inaccessible from the defaulted assignment operator
  • The implicitly-defined copy/move assignment operator for anon-union class X performs member wise copy/move assignment of its subobjects. The direct base classes of X are assigned first, in the order of their declaration in the base-specifier-list, and then the immediate non-static data members of X are assigned, in the order in which they were declared in the class definition. Let x be either the parameter of the function or, for the move operator, an xvalue referring to the parameter. Each subobject is assigned in the manner appropriate to its type:
    • if the subobject is of class type, as if by a call to operator= with the subobject as the object expression and the corresponding subobject of x as a single function argument (as if by explicit qualification; that is, ignoring any possible virtual overriding functions in more derived classes);
    • if the subobject is an array, each element is assigned, in the manner appropriate to the element type;
    • if the subobject is of scalar type, the built-in assignment operator is used.
  • It is unspecified whether subobjects representing virtual base classes are assigned more than once by the implicitly-defined copy assignment operator. Example:
    struct V { };
    struct A : virtual V { };
    struct B : virtual V { };
    struct C : B, A { };
    It is unspecified whether the virtual base class subobject V is assigned twice by the implicitly-defined copy assignment operator for C. [Note: This does not apply to move assignment, as a defaulted move assignment operator is deleted if the class has virtual bases].
  • The implicitly-defined copy assignment operator for a union X copies the object representation of X.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值