c++c++11rule-of-three

Rule of 3 Default Member Deprecation in C++11


According to the below widely-known table, automatic compiler generation of default copy constructor and copy assignment is deprecated in C++11 when one or more of the copy assignment, copy constructor, and destructor is/are supplied by the user (the red cells indicate deprecation). This makes perfect sense in light of the "Rule of 3". However, the table shows that generation of the default destructor is not deprecated in the case of user-supplied copy constructor/assignment.

What is the rationale behind this design decision?

Reference Table


Solution

  • Why should it be deprecated? It's perfectly possible for an object to require special copying properties, but its for destruction to be fully determined by its sub-object destructors. Consider a simple cloning pointer:

    template <class T>
    class cloning_ptr
    {
      std::unique_ptr<T> p;
    
    public:
      cloning_ptr(const cloning_ptr &src) : p(std::make_unique<T>(*src.p) {}
      cloning_ptr(cloning_ptr &&) = default;
    
      cloning_ptr& operator= (cloning_ptr rhs)
      { swap(p, rhs.p); return *this; }    
    };
    

    There's zero reason to provide a destructor that does anything different from the defaulted one.

    The other way around is different: if you need to do special things in a dtor, that probably means there is some non-standard ownership modeled in the class. Non-standard ownership will most likely need handling in copy operations, too.