An interesting continuation - why?
In C ++ 98 there was a Rule of Three :
If you define any of the following, you must define all three:
- destructor
- copy constructor
This rule of thumb was created because many people only thought about releasing the resource stored in the destructor and forgot about the consequences that this special behavior had on the copies.
When C ++ 11 appeared around the corner, a number of people claimed that this problem was caused by the default definition provided by the language, and that it would be better to look back so as not to provide them by default. Of course, C provides them by default (for struct
), so ...
... some suggested that the rule Three could actually be executed by the compiler; or, at least, since changing existing behavior can break existing code, that the rule 3 pendant can be applied by the compiler whenever it comes to a move constructor or a move assignment operator (which guarantees new C ++ 11 code) .
Rule of Five:
If you define one of the following values, you must define all five:
- destructor
- move constructor
- copy constructor
Thus,
was almost completely implemented as:
- If you define a move constructor or move an assignment operator, the remaining 4 methods are implicitly removed (unless you provide them)
- If you define a destructor, copy constructor, or copy assignment operator, the move constructor and move assignment operator are implicitly deleted (unless you provide them)
the second statement is slightly incomplete for reasons of backward compatibility with existing C ++ 98 code (which should compile as C ++ 11 without changing behavior).
source share