Relative Content

Tag Archive for rule-of-three

Is the rule of 5 a valid extension of the rule of 3, or does it imply premature optimization?

I’m familiar with the notion in c++ of the rule of 3, however since the release of C++11 I’ve seen some sources suggesting it should be extended to a “rule of 5”, I.e. the move constructor and move assignment operator should also be implemented whenever the others are. What is the rationale behind such a rule? My understanding is that in most cases implementation of move semantics is only necessary as an optimization – am I wrong about this, or is the so-called rule of five about optimizing my code (and, therefore, substantially less important than the rule of 3, which is about avoiding pitfalls that can lead to unexpected behaviors)?

Is the rule of 5 a valid extension of the rule of 3, or does it imply premature optimization?

I’m familiar with the notion in c++ of the rule of 3, however since the release of C++11 I’ve seen some sources suggesting it should be extended to a “rule of 5”, I.e. the move constructor and move assignment operator should also be implemented whenever the others are. What is the rationale behind such a rule? My understanding is that in most cases implementation of move semantics is only necessary as an optimization – am I wrong about this, or is the so-called rule of five about optimizing my code (and, therefore, substantially less important than the rule of 3, which is about avoiding pitfalls that can lead to unexpected behaviors)?