Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator?

后端 未结 3 998
旧巷少年郎
旧巷少年郎 2020-11-22 03:57

I want to refresh my memory on the conditions under which a compiler typically auto generates a default constructor, copy constructor and assignment operator.

I reco

3条回答
  •  名媛妹妹
    2020-11-22 04:57

    In the following, "auto-generated" means "implicitly declared as defaulted, but not defined as deleted". There are situations where the special member functions are declared, but defined as deleted.

    • The default constructor is auto-generated if there is no user-declared constructor (§12.1/5).
    • The copy constructor is auto-generated if there is no user-declared move constructor or move assignment operator (because there are no move constructors or move assignment operators in C++03, this simplifies to "always" in C++03) (§12.8/8).
    • The copy assignment operator is auto-generated if there is no user-declared move constructor or move assignment operator (§12.8/19).
    • The destructor is auto-generated if there is no user-declared destructor (§12.4/4).

    C++11 and later only:

    • The move constructor is auto-generated if there is no user-declared copy constructor, copy assignment operator or destructor, and if the generated move constructor is valid (§12.8/10).
    • The move assignment operator is auto-generated if there is no user-declared copy constructor, copy assignment operator or destructor, and if the generated move assignment operator is valid (e.g. if it wouldn't need to assign constant members) (§12.8/21).

提交回复
热议问题