Upon Lightness Races in Orbit's clarification, I've narrowed my post.
After reading this article: The Rule of Zero,
I came to understand the most, but I still want to solve some unclear issues that I have:
1. Looking at this phrase:
If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if:
X does not have a user-declared copy constructor, and
X does not have a user-declared copy assignment operator,
X does not have a user-declared move assignment operator,
X does not have a user-declared destructor, and
The move constructor would not be implicitly defined as deleted.
Should all 5 statements coexist (share an "and" relation) or only some of them(share an "or" relation)?
2. What "user-declared" copy constructor\copy assignment operator... means?
is declaring it (any one of the list above) in an .h file but not implementing it considered user-declared?
is declaring it (any one of the list above) in an .h file and specify "=deleted" or "=default" considered user-declared?
is declaring it (any one of the list above) in an .h file with empty bracelets ,{}, considered user-declared?
Respectfully,
Etay
The rule of three and rule of five are rules of thumb in C++ for the building of exception-safe code and for formalizing rules on resource management. The rules prescribe how the default members of a class should be used to achieve these goals systematically.
The rule of 5 states that if a class has a user-declared destructor, copy constructor, copy assignment constructor, move constructor, or move assignment constructor, then it must have the other 4.
The copy-and-swap idiom provides an elegant technique to avoid these problems. It utilizes the copy constructor to create a temporary object, and exchanges its contents with itself using a non-throwing swap. Therefore, it swaps the old data with new data. The temporary object is then destructed automatically (RAII).
This rule basically states that if a class defines one (or more) of the following, it should explicitly define all three, which are: destructor. copy constructor. copy assignment operator.
A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .
A user declared constructor is a constructor that has been written by the programmer, and not added by the compiler. User defined is the opposite of default or implicit in this case.
Your class will have a implicit default move constructor unless any of that conditions happen in your class. So, it is a "negative and". None of them must happen to get an implicit default move constructor.
In all of your mentioned cases, the constructor is used declared, even when deleted.
The reason for these rules are for retro-compatibility with pre C++-11. When a user declared a copy constructor, temporaries were send to them too. If you go to a C++11 compiler, and move constructors were indiscriminately implicit, the behaviour would change. Calls that went before to the copy constructor, now go to a phantom move constructor the user could be not aware of.
So, each time the compiler sees a copy constructor or an assignment operator (meaning the class manages its own resources), the behaviour fallbacks to pre C++11 and move constructors aren't considered.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With