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










107















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 recollect there were some rules, but I don't remember, and also can't find a reputable resource online. Can anyone help?










share|improve this question




























    107















    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 recollect there were some rules, but I don't remember, and also can't find a reputable resource online. Can anyone help?










    share|improve this question


























      107












      107








      107


      69






      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 recollect there were some rules, but I don't remember, and also can't find a reputable resource online. Can anyone help?










      share|improve this question
















      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 recollect there were some rules, but I don't remember, and also can't find a reputable resource online. Can anyone help?







      c++ copy-constructor default-constructor move-constructor move-assignment-operator






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 19 '18 at 10:39









      Community

      11




      11










      asked Feb 9 '11 at 11:01









      oompahloompahoompahloompah

      3,623165386




      3,623165386






















          3 Answers
          3






          active

          oldest

          votes


















          121














          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).





          share|improve this answer




















          • 7





            Does an inherited destructor count? I mean, say I've got a base class with an empty virtual destructor. Does it prevent creation of move constructors in subclasses? If the answer is yes, will it help if I define a move constructor in the base class?

            – kamilk
            Jul 6 '14 at 13:29







          • 8





            I think that you should mention perhaps that having const members in the class will prevent the constructor from being auto-generated...

            – nonsensickle
            Jul 31 '14 at 23:08











          • Does "There are situations where the special member functions are declared, but defined as deleted." refer to where you for example have const or reference members where move will be impossible? No, that can't be, because there copy will be applied.

            – towi
            Sep 27 '16 at 7:37












          • I know that it's restricted to send hyperlinks in this forum. But it's also good article - cplusplus.com/articles/y8hv0pDG

            – bruziuz
            Oct 12 '16 at 0:54











          • Note, that as of the standard an implicitly defaulted copy constructor "is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor" (12.8 Copying and moving class objects [class.copy]).

            – sigy
            Mar 29 '17 at 10:59



















          69














          I've found the diagram below very useful.



          C++ rules for automatic constructors and assignment operators
          from Sticky Bits - Becoming a Rule of Zero Hero






          share|improve this answer

























          • Beautiful. What does "independent" refer to? Independent from what?

            – towi
            Sep 27 '16 at 7:29






          • 4





            Copy ctor/assignment are 'independent' from each other. If you write just one, the compiler will provide the other. In contrast, if you provide either a move ctor or a move assignment, the compiler won't supply the other.

            – Marco M.
            Oct 3 '16 at 20:14











          • Wonder what's the reason behind copy operations are being independent. Historic reasons may be? or the fact that copy won't modify it's target but move does?

            – Explorer_N
            Jul 5 '17 at 7:47












          • @Explorer_N Yes, backward compatibility, so historic reasons. It was a bad design choice long time ago, so now there's a need for good practices like the "rule of three" (define all 3 or none: copy constructor, copy assignment operator, and often destructor) to avoid hard to find bugs.

            – atablash
            Mar 31 '18 at 18:41


















          1














          C++17 N4659 standard draft



          For a quick cross standard reference, have a look at the "Implicitly-declared" sections of the following cppreference entries:



          • https://en.cppreference.com/w/cpp/language/copy_constructor

          • https://en.cppreference.com/w/cpp/language/move_constructor

          • https://en.cppreference.com/w/cpp/language/copy_assignment

          • https://en.cppreference.com/w/cpp/language/move_assignment

          The same information can of course be obtained from the standard. E.g. on C++17 N4659 standard draft:



          15.8.1 "Copy/move constructors" says for for copy constructor:




          6 If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly.
          If the class definition declares a move constructor or move assignment operator, the implicitly declared copy
          constructor is defined as deleted; otherwise, it is defined as defaulted (11.4). The latter case is deprecated if
          the class has a user-declared copy assignment operator or a user-declared destructor.




          and for move constructor:




          8 If the definition of a class X does not explicitly declare a move constructor, a non-explicit one will be implicitly
          declared as defaulted if and only if



          • (8.1)
            — X does not have a user-declared copy constructor,


          • (8.2)
            — X does not have a user-declared copy assignment operator,


          • (8.3)
            — X does not have a user-declared move assignment operator, and


          • (8.4)
            — X does not have a user-declared destructor.




          15.8.2 "Copy/move assignment operator" says for copy assignment:




          2 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 (11.4). The latter
          case is deprecated if the class has a user-declared copy constructor or a user-declared destructor.




          and for move assignment:




          4 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



          • (4.1) — X does not have a user-declared copy constructor,

          • (4.2) — X does not have a user-declared move constructor,

          • (4.3) — X does not have a user-declared copy assignment operator, and

          • (4.4) — X does not have a user-declared destructor.



          15.4 "Destructors" says it for destructors:




          4 If a class has no user-declared destructor, a destructor is implicitly declared as defaulted (11.4). An
          implicitly-declared destructor is an inline public member of its class.







          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4943958%2fconditions-for-automatic-generation-of-default-copy-move-ctor-and-copy-move-assi%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            121














            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).





            share|improve this answer




















            • 7





              Does an inherited destructor count? I mean, say I've got a base class with an empty virtual destructor. Does it prevent creation of move constructors in subclasses? If the answer is yes, will it help if I define a move constructor in the base class?

              – kamilk
              Jul 6 '14 at 13:29







            • 8





              I think that you should mention perhaps that having const members in the class will prevent the constructor from being auto-generated...

              – nonsensickle
              Jul 31 '14 at 23:08











            • Does "There are situations where the special member functions are declared, but defined as deleted." refer to where you for example have const or reference members where move will be impossible? No, that can't be, because there copy will be applied.

              – towi
              Sep 27 '16 at 7:37












            • I know that it's restricted to send hyperlinks in this forum. But it's also good article - cplusplus.com/articles/y8hv0pDG

              – bruziuz
              Oct 12 '16 at 0:54











            • Note, that as of the standard an implicitly defaulted copy constructor "is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor" (12.8 Copying and moving class objects [class.copy]).

              – sigy
              Mar 29 '17 at 10:59
















            121














            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).





            share|improve this answer




















            • 7





              Does an inherited destructor count? I mean, say I've got a base class with an empty virtual destructor. Does it prevent creation of move constructors in subclasses? If the answer is yes, will it help if I define a move constructor in the base class?

              – kamilk
              Jul 6 '14 at 13:29







            • 8





              I think that you should mention perhaps that having const members in the class will prevent the constructor from being auto-generated...

              – nonsensickle
              Jul 31 '14 at 23:08











            • Does "There are situations where the special member functions are declared, but defined as deleted." refer to where you for example have const or reference members where move will be impossible? No, that can't be, because there copy will be applied.

              – towi
              Sep 27 '16 at 7:37












            • I know that it's restricted to send hyperlinks in this forum. But it's also good article - cplusplus.com/articles/y8hv0pDG

              – bruziuz
              Oct 12 '16 at 0:54











            • Note, that as of the standard an implicitly defaulted copy constructor "is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor" (12.8 Copying and moving class objects [class.copy]).

              – sigy
              Mar 29 '17 at 10:59














            121












            121








            121







            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).





            share|improve this answer















            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).






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 24 '17 at 21:35









            milleniumbug

            12.7k33462




            12.7k33462










            answered Feb 9 '11 at 11:17









            PhilippPhilipp

            37.6k107097




            37.6k107097







            • 7





              Does an inherited destructor count? I mean, say I've got a base class with an empty virtual destructor. Does it prevent creation of move constructors in subclasses? If the answer is yes, will it help if I define a move constructor in the base class?

              – kamilk
              Jul 6 '14 at 13:29







            • 8





              I think that you should mention perhaps that having const members in the class will prevent the constructor from being auto-generated...

              – nonsensickle
              Jul 31 '14 at 23:08











            • Does "There are situations where the special member functions are declared, but defined as deleted." refer to where you for example have const or reference members where move will be impossible? No, that can't be, because there copy will be applied.

              – towi
              Sep 27 '16 at 7:37












            • I know that it's restricted to send hyperlinks in this forum. But it's also good article - cplusplus.com/articles/y8hv0pDG

              – bruziuz
              Oct 12 '16 at 0:54











            • Note, that as of the standard an implicitly defaulted copy constructor "is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor" (12.8 Copying and moving class objects [class.copy]).

              – sigy
              Mar 29 '17 at 10:59













            • 7





              Does an inherited destructor count? I mean, say I've got a base class with an empty virtual destructor. Does it prevent creation of move constructors in subclasses? If the answer is yes, will it help if I define a move constructor in the base class?

              – kamilk
              Jul 6 '14 at 13:29







            • 8





              I think that you should mention perhaps that having const members in the class will prevent the constructor from being auto-generated...

              – nonsensickle
              Jul 31 '14 at 23:08











            • Does "There are situations where the special member functions are declared, but defined as deleted." refer to where you for example have const or reference members where move will be impossible? No, that can't be, because there copy will be applied.

              – towi
              Sep 27 '16 at 7:37












            • I know that it's restricted to send hyperlinks in this forum. But it's also good article - cplusplus.com/articles/y8hv0pDG

              – bruziuz
              Oct 12 '16 at 0:54











            • Note, that as of the standard an implicitly defaulted copy constructor "is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor" (12.8 Copying and moving class objects [class.copy]).

              – sigy
              Mar 29 '17 at 10:59








            7




            7





            Does an inherited destructor count? I mean, say I've got a base class with an empty virtual destructor. Does it prevent creation of move constructors in subclasses? If the answer is yes, will it help if I define a move constructor in the base class?

            – kamilk
            Jul 6 '14 at 13:29






            Does an inherited destructor count? I mean, say I've got a base class with an empty virtual destructor. Does it prevent creation of move constructors in subclasses? If the answer is yes, will it help if I define a move constructor in the base class?

            – kamilk
            Jul 6 '14 at 13:29





            8




            8





            I think that you should mention perhaps that having const members in the class will prevent the constructor from being auto-generated...

            – nonsensickle
            Jul 31 '14 at 23:08





            I think that you should mention perhaps that having const members in the class will prevent the constructor from being auto-generated...

            – nonsensickle
            Jul 31 '14 at 23:08













            Does "There are situations where the special member functions are declared, but defined as deleted." refer to where you for example have const or reference members where move will be impossible? No, that can't be, because there copy will be applied.

            – towi
            Sep 27 '16 at 7:37






            Does "There are situations where the special member functions are declared, but defined as deleted." refer to where you for example have const or reference members where move will be impossible? No, that can't be, because there copy will be applied.

            – towi
            Sep 27 '16 at 7:37














            I know that it's restricted to send hyperlinks in this forum. But it's also good article - cplusplus.com/articles/y8hv0pDG

            – bruziuz
            Oct 12 '16 at 0:54





            I know that it's restricted to send hyperlinks in this forum. But it's also good article - cplusplus.com/articles/y8hv0pDG

            – bruziuz
            Oct 12 '16 at 0:54













            Note, that as of the standard an implicitly defaulted copy constructor "is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor" (12.8 Copying and moving class objects [class.copy]).

            – sigy
            Mar 29 '17 at 10:59






            Note, that as of the standard an implicitly defaulted copy constructor "is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor" (12.8 Copying and moving class objects [class.copy]).

            – sigy
            Mar 29 '17 at 10:59














            69














            I've found the diagram below very useful.



            C++ rules for automatic constructors and assignment operators
            from Sticky Bits - Becoming a Rule of Zero Hero






            share|improve this answer

























            • Beautiful. What does "independent" refer to? Independent from what?

              – towi
              Sep 27 '16 at 7:29






            • 4





              Copy ctor/assignment are 'independent' from each other. If you write just one, the compiler will provide the other. In contrast, if you provide either a move ctor or a move assignment, the compiler won't supply the other.

              – Marco M.
              Oct 3 '16 at 20:14











            • Wonder what's the reason behind copy operations are being independent. Historic reasons may be? or the fact that copy won't modify it's target but move does?

              – Explorer_N
              Jul 5 '17 at 7:47












            • @Explorer_N Yes, backward compatibility, so historic reasons. It was a bad design choice long time ago, so now there's a need for good practices like the "rule of three" (define all 3 or none: copy constructor, copy assignment operator, and often destructor) to avoid hard to find bugs.

              – atablash
              Mar 31 '18 at 18:41















            69














            I've found the diagram below very useful.



            C++ rules for automatic constructors and assignment operators
            from Sticky Bits - Becoming a Rule of Zero Hero






            share|improve this answer

























            • Beautiful. What does "independent" refer to? Independent from what?

              – towi
              Sep 27 '16 at 7:29






            • 4





              Copy ctor/assignment are 'independent' from each other. If you write just one, the compiler will provide the other. In contrast, if you provide either a move ctor or a move assignment, the compiler won't supply the other.

              – Marco M.
              Oct 3 '16 at 20:14











            • Wonder what's the reason behind copy operations are being independent. Historic reasons may be? or the fact that copy won't modify it's target but move does?

              – Explorer_N
              Jul 5 '17 at 7:47












            • @Explorer_N Yes, backward compatibility, so historic reasons. It was a bad design choice long time ago, so now there's a need for good practices like the "rule of three" (define all 3 or none: copy constructor, copy assignment operator, and often destructor) to avoid hard to find bugs.

              – atablash
              Mar 31 '18 at 18:41













            69












            69








            69







            I've found the diagram below very useful.



            C++ rules for automatic constructors and assignment operators
            from Sticky Bits - Becoming a Rule of Zero Hero






            share|improve this answer















            I've found the diagram below very useful.



            C++ rules for automatic constructors and assignment operators
            from Sticky Bits - Becoming a Rule of Zero Hero







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 20 '18 at 11:57









            O'Neil

            3,35721126




            3,35721126










            answered Jul 8 '16 at 1:02









            Marco M.Marco M.

            1,64011920




            1,64011920












            • Beautiful. What does "independent" refer to? Independent from what?

              – towi
              Sep 27 '16 at 7:29






            • 4





              Copy ctor/assignment are 'independent' from each other. If you write just one, the compiler will provide the other. In contrast, if you provide either a move ctor or a move assignment, the compiler won't supply the other.

              – Marco M.
              Oct 3 '16 at 20:14











            • Wonder what's the reason behind copy operations are being independent. Historic reasons may be? or the fact that copy won't modify it's target but move does?

              – Explorer_N
              Jul 5 '17 at 7:47












            • @Explorer_N Yes, backward compatibility, so historic reasons. It was a bad design choice long time ago, so now there's a need for good practices like the "rule of three" (define all 3 or none: copy constructor, copy assignment operator, and often destructor) to avoid hard to find bugs.

              – atablash
              Mar 31 '18 at 18:41

















            • Beautiful. What does "independent" refer to? Independent from what?

              – towi
              Sep 27 '16 at 7:29






            • 4





              Copy ctor/assignment are 'independent' from each other. If you write just one, the compiler will provide the other. In contrast, if you provide either a move ctor or a move assignment, the compiler won't supply the other.

              – Marco M.
              Oct 3 '16 at 20:14











            • Wonder what's the reason behind copy operations are being independent. Historic reasons may be? or the fact that copy won't modify it's target but move does?

              – Explorer_N
              Jul 5 '17 at 7:47












            • @Explorer_N Yes, backward compatibility, so historic reasons. It was a bad design choice long time ago, so now there's a need for good practices like the "rule of three" (define all 3 or none: copy constructor, copy assignment operator, and often destructor) to avoid hard to find bugs.

              – atablash
              Mar 31 '18 at 18:41
















            Beautiful. What does "independent" refer to? Independent from what?

            – towi
            Sep 27 '16 at 7:29





            Beautiful. What does "independent" refer to? Independent from what?

            – towi
            Sep 27 '16 at 7:29




            4




            4





            Copy ctor/assignment are 'independent' from each other. If you write just one, the compiler will provide the other. In contrast, if you provide either a move ctor or a move assignment, the compiler won't supply the other.

            – Marco M.
            Oct 3 '16 at 20:14





            Copy ctor/assignment are 'independent' from each other. If you write just one, the compiler will provide the other. In contrast, if you provide either a move ctor or a move assignment, the compiler won't supply the other.

            – Marco M.
            Oct 3 '16 at 20:14













            Wonder what's the reason behind copy operations are being independent. Historic reasons may be? or the fact that copy won't modify it's target but move does?

            – Explorer_N
            Jul 5 '17 at 7:47






            Wonder what's the reason behind copy operations are being independent. Historic reasons may be? or the fact that copy won't modify it's target but move does?

            – Explorer_N
            Jul 5 '17 at 7:47














            @Explorer_N Yes, backward compatibility, so historic reasons. It was a bad design choice long time ago, so now there's a need for good practices like the "rule of three" (define all 3 or none: copy constructor, copy assignment operator, and often destructor) to avoid hard to find bugs.

            – atablash
            Mar 31 '18 at 18:41





            @Explorer_N Yes, backward compatibility, so historic reasons. It was a bad design choice long time ago, so now there's a need for good practices like the "rule of three" (define all 3 or none: copy constructor, copy assignment operator, and often destructor) to avoid hard to find bugs.

            – atablash
            Mar 31 '18 at 18:41











            1














            C++17 N4659 standard draft



            For a quick cross standard reference, have a look at the "Implicitly-declared" sections of the following cppreference entries:



            • https://en.cppreference.com/w/cpp/language/copy_constructor

            • https://en.cppreference.com/w/cpp/language/move_constructor

            • https://en.cppreference.com/w/cpp/language/copy_assignment

            • https://en.cppreference.com/w/cpp/language/move_assignment

            The same information can of course be obtained from the standard. E.g. on C++17 N4659 standard draft:



            15.8.1 "Copy/move constructors" says for for copy constructor:




            6 If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly.
            If the class definition declares a move constructor or move assignment operator, the implicitly declared copy
            constructor is defined as deleted; otherwise, it is defined as defaulted (11.4). The latter case is deprecated if
            the class has a user-declared copy assignment operator or a user-declared destructor.




            and for move constructor:




            8 If the definition of a class X does not explicitly declare a move constructor, a non-explicit one will be implicitly
            declared as defaulted if and only if



            • (8.1)
              — X does not have a user-declared copy constructor,


            • (8.2)
              — X does not have a user-declared copy assignment operator,


            • (8.3)
              — X does not have a user-declared move assignment operator, and


            • (8.4)
              — X does not have a user-declared destructor.




            15.8.2 "Copy/move assignment operator" says for copy assignment:




            2 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 (11.4). The latter
            case is deprecated if the class has a user-declared copy constructor or a user-declared destructor.




            and for move assignment:




            4 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



            • (4.1) — X does not have a user-declared copy constructor,

            • (4.2) — X does not have a user-declared move constructor,

            • (4.3) — X does not have a user-declared copy assignment operator, and

            • (4.4) — X does not have a user-declared destructor.



            15.4 "Destructors" says it for destructors:




            4 If a class has no user-declared destructor, a destructor is implicitly declared as defaulted (11.4). An
            implicitly-declared destructor is an inline public member of its class.







            share|improve this answer



























              1














              C++17 N4659 standard draft



              For a quick cross standard reference, have a look at the "Implicitly-declared" sections of the following cppreference entries:



              • https://en.cppreference.com/w/cpp/language/copy_constructor

              • https://en.cppreference.com/w/cpp/language/move_constructor

              • https://en.cppreference.com/w/cpp/language/copy_assignment

              • https://en.cppreference.com/w/cpp/language/move_assignment

              The same information can of course be obtained from the standard. E.g. on C++17 N4659 standard draft:



              15.8.1 "Copy/move constructors" says for for copy constructor:




              6 If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly.
              If the class definition declares a move constructor or move assignment operator, the implicitly declared copy
              constructor is defined as deleted; otherwise, it is defined as defaulted (11.4). The latter case is deprecated if
              the class has a user-declared copy assignment operator or a user-declared destructor.




              and for move constructor:




              8 If the definition of a class X does not explicitly declare a move constructor, a non-explicit one will be implicitly
              declared as defaulted if and only if



              • (8.1)
                — X does not have a user-declared copy constructor,


              • (8.2)
                — X does not have a user-declared copy assignment operator,


              • (8.3)
                — X does not have a user-declared move assignment operator, and


              • (8.4)
                — X does not have a user-declared destructor.




              15.8.2 "Copy/move assignment operator" says for copy assignment:




              2 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 (11.4). The latter
              case is deprecated if the class has a user-declared copy constructor or a user-declared destructor.




              and for move assignment:




              4 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



              • (4.1) — X does not have a user-declared copy constructor,

              • (4.2) — X does not have a user-declared move constructor,

              • (4.3) — X does not have a user-declared copy assignment operator, and

              • (4.4) — X does not have a user-declared destructor.



              15.4 "Destructors" says it for destructors:




              4 If a class has no user-declared destructor, a destructor is implicitly declared as defaulted (11.4). An
              implicitly-declared destructor is an inline public member of its class.







              share|improve this answer

























                1












                1








                1







                C++17 N4659 standard draft



                For a quick cross standard reference, have a look at the "Implicitly-declared" sections of the following cppreference entries:



                • https://en.cppreference.com/w/cpp/language/copy_constructor

                • https://en.cppreference.com/w/cpp/language/move_constructor

                • https://en.cppreference.com/w/cpp/language/copy_assignment

                • https://en.cppreference.com/w/cpp/language/move_assignment

                The same information can of course be obtained from the standard. E.g. on C++17 N4659 standard draft:



                15.8.1 "Copy/move constructors" says for for copy constructor:




                6 If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly.
                If the class definition declares a move constructor or move assignment operator, the implicitly declared copy
                constructor is defined as deleted; otherwise, it is defined as defaulted (11.4). The latter case is deprecated if
                the class has a user-declared copy assignment operator or a user-declared destructor.




                and for move constructor:




                8 If the definition of a class X does not explicitly declare a move constructor, a non-explicit one will be implicitly
                declared as defaulted if and only if



                • (8.1)
                  — X does not have a user-declared copy constructor,


                • (8.2)
                  — X does not have a user-declared copy assignment operator,


                • (8.3)
                  — X does not have a user-declared move assignment operator, and


                • (8.4)
                  — X does not have a user-declared destructor.




                15.8.2 "Copy/move assignment operator" says for copy assignment:




                2 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 (11.4). The latter
                case is deprecated if the class has a user-declared copy constructor or a user-declared destructor.




                and for move assignment:




                4 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



                • (4.1) — X does not have a user-declared copy constructor,

                • (4.2) — X does not have a user-declared move constructor,

                • (4.3) — X does not have a user-declared copy assignment operator, and

                • (4.4) — X does not have a user-declared destructor.



                15.4 "Destructors" says it for destructors:




                4 If a class has no user-declared destructor, a destructor is implicitly declared as defaulted (11.4). An
                implicitly-declared destructor is an inline public member of its class.







                share|improve this answer













                C++17 N4659 standard draft



                For a quick cross standard reference, have a look at the "Implicitly-declared" sections of the following cppreference entries:



                • https://en.cppreference.com/w/cpp/language/copy_constructor

                • https://en.cppreference.com/w/cpp/language/move_constructor

                • https://en.cppreference.com/w/cpp/language/copy_assignment

                • https://en.cppreference.com/w/cpp/language/move_assignment

                The same information can of course be obtained from the standard. E.g. on C++17 N4659 standard draft:



                15.8.1 "Copy/move constructors" says for for copy constructor:




                6 If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly.
                If the class definition declares a move constructor or move assignment operator, the implicitly declared copy
                constructor is defined as deleted; otherwise, it is defined as defaulted (11.4). The latter case is deprecated if
                the class has a user-declared copy assignment operator or a user-declared destructor.




                and for move constructor:




                8 If the definition of a class X does not explicitly declare a move constructor, a non-explicit one will be implicitly
                declared as defaulted if and only if



                • (8.1)
                  — X does not have a user-declared copy constructor,


                • (8.2)
                  — X does not have a user-declared copy assignment operator,


                • (8.3)
                  — X does not have a user-declared move assignment operator, and


                • (8.4)
                  — X does not have a user-declared destructor.




                15.8.2 "Copy/move assignment operator" says for copy assignment:




                2 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 (11.4). The latter
                case is deprecated if the class has a user-declared copy constructor or a user-declared destructor.




                and for move assignment:




                4 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



                • (4.1) — X does not have a user-declared copy constructor,

                • (4.2) — X does not have a user-declared move constructor,

                • (4.3) — X does not have a user-declared copy assignment operator, and

                • (4.4) — X does not have a user-declared destructor.



                15.4 "Destructors" says it for destructors:




                4 If a class has no user-declared destructor, a destructor is implicitly declared as defaulted (11.4). An
                implicitly-declared destructor is an inline public member of its class.








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 9:09









                Ciro Santilli 新疆改造中心 六四事件 法轮功Ciro Santilli 新疆改造中心 六四事件 法轮功

                145k34555471




                145k34555471



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4943958%2fconditions-for-automatic-generation-of-default-copy-move-ctor-and-copy-move-assi%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                    Museum of Modern and Contemporary Art of Trento and Rovereto