Rule of three (C++ programming)

From Wikipedia, the free encyclopedia
Jump to: navigation, search

The rule of three, rule of five, and rule of 0 are rules of thumb in C++ for the building of exception-safe code and for formalizing rules on resource management. It accomplishes this by prescribing how the default members of a class should be used to accomplish this task in a systematic manner.

Rule of Three[edit]

The rule of three (also known as the Law of The Big Three or The Big Three) is a rule of thumb in C++ (prior to C++11) that claims that if a class defines one of the following it should probably explicitly define all three:[1]

These three functions are special member functions. If one of these functions is used without first being declared by the programmer it will be implicitly implemented by the compiler with the default semantics of performing the said operation on all the members of the class. The default semantics are:

  • Destructor – Call the destructors of all the object's class-type members
  • Copy constructor – Construct all the object's members from the corresponding members of the copy constructor's argument, calling the copy constructors of the object's class-type members, and doing a plain assignment of all non-class type (e.g., int or pointer) data members
  • Copy assignment operator – Assign all the object's members from the corresponding members of the assignment operator's argument, calling the copy assignment operators of the object's class-type members, and doing a plain assignment of all non-class type (e.g. int or pointer) data members.

The Rule of Three claims that if one of these had to be defined by the programmer, it means that the compiler-generated version does not fit the needs of the class in one case and it will probably not fit in the other cases either. The term "Rule of three" was coined by Marshall Cline in 1991.[2]

An amendment to this rule is that if Resource Acquisition Is Initialization (RAII) is used for the class members, the destructor may be left undefined (also known as The Law of The Big Two[3]).

Because implicitly-generated constructors and assignment operators simply copy all class data members,[4] one should define explicit copy constructors and copy assignment operators for classes that encapsulate complex data structures or have external references such as pointers, since only the pointer gets copied, not the object it points to. In the case that this default behavior is actually the intended behavior, an explicit declaration can prevent ambiguity.

Rule of 5[edit]

With the advent of C++11 the rule of three probably needs to be broadened to the rule of five as C++11 implements move semantics,[5] allowing destination objects to grab (or steal) data from temporary objects. The following example also shows the new moving members: move constructor and move assignment operator. Consequently, for the rule of five we have the following special members:

Note also that situations exist where classes may need destructors, but cannot sensibly implement copy and move constructors and copy and move assignment operators. This happens, for example, when the base class does not support these latter Big Four members, but the derived class's constructor allocates memory for its own use.[citation needed] In C++11, this can be simplified by explicitly specifying the five members as default.[7]

Rule of 0[edit]

There's a proposal by R. Martinho Fernandes to simplify all of the above into a Rule of 0 for C++ (primarily for C++11 & newer).[8] The rule of 0 states that if you specify any of the default members, then your class must deal exclusively with a single resource. Furthermore, it must define all default members to handle that resource (or delete the default member as appropriate). Thus such classes must follow the rule of 5 described above. A resource can be anything: memory that gets allocated, a file descriptor, database transaction etc.

Any other class must not allocate any resources directly. Furthermore, they must omit the default members (or explicitly assign all of them to default via = default). Any resources should be used indirectly by using the single-resource classes as member/local variables. This lets such classes inherit the default members from the union of member variables, thereby auto-forwarding the movability/copyability of the union of all underlying resource. Since ownership of 1 resource is owned by exactly 1 member variable, exceptions in the constructor cannot leak resources due to RAII. Fully initialized variables will have their destructors called & uninitialized variables could not have owned any resources to begin with.

Since the majority of classes don't deal with ownership as their sole concern, the majority of classes can omit the default members. This is where the rule-of-0 gets its name.

Example in C++[edit]

#include <cstring>
#include <iostream>
 
class Foo
{
public:
    /** Constructor */
    Foo() :
        data (new char[14])
    {
        std::strcpy (data, "Hello, World!");
    }
 
    /** Copy Constructor */
    Foo (const Foo& other) :
        data (new char[std::strlen (other.data) + 1])
    {
        std::strcpy (data, other.data);
    }
 
    /** Move Constructor */
    Foo (Foo&& other) noexcept : /* noexcept needed to enable optimizations in containers */
        data(other.data)
    {
        other.data = nullptr;
    }
 
    /** Destructor */
    ~Foo() noexcept /* explicitly specified destructors should be annotated noexcept as best-practice */
    {
        delete[] data;
    }
 
    /** Copy Assignment Operator */
    Foo& operator= (const Foo& other)
    {
        Foo tmp(other); // re-use copy-constructor
        *this = std::move(tmp); // re-use move-assignment
        return *this;
    }
 
    /** Move Assignment Operator */
    Foo& operator= (Foo&& other) noexcept
    {
        // simplified move-constructor that also protects against move-to-self.
        // see http://scottmeyers.blogspot.com/2014/06/the-drawbacks-of-implementing-move.html
        // for how to implement a more-optimized version of this.
        using namespace std;
        swap(data, other.data); // repeat for all elements
        return *this;
    }
 
private:
    friend std::ostream& operator<< (std::ostream& os, const Foo& foo)
    {
        os << foo.data;
        return os;
    }
 
    char* data;		
};
 
int main()
{
    const Foo foo;
    std::cout << foo << std::endl;
 
    return 0;
}

See also[edit]

References[edit]

  1. ^ Stroustrup, Bjarne (2000). The C++ Programming Language (3 ed.). Addison-Wesley. pp. 283–4. ISBN 978-0-201-70073-2. 
  2. ^ Koenig, Andrew; Barbara E. Moo (2001-06-01). "C++ Made Easier: The Rule of Three". Dr. Dobb's Journal. Retrieved 2009-09-08. 
  3. ^ Karlsson, Bjorn; Wilson, Matthew (2004-10-01). "The Law of the Big Two". The C++ Source. Artima. Retrieved 2008-01-22. 
  4. ^ The C++ Programming Language. p. 271. 
  5. ^ Stroustrup, Bjarne (2013-04-07). "C++11 - the new ISO C++ standard". Retrieved 2013-05-10. 
  6. ^ "Move Assignment Operator". En.CPPReference.com. Retrieved 2014-12-22. 
  7. ^ http://en.cppreference.com/w/cpp/language/rule_of_three
  8. ^ http://flamingdangerzone.com/cxx11/2012/08/15/rule-of-zero.html