See also #2164 where this issue is creating false positives in Guidelines checkers such as clang-tidy.
Problem
C.21 mandates the Rule of Five:
C.21: If you define or =delete any copy, move, or destructor function, define or =delete them all
[...]
Enforcement
(Simple) A class should have a declaration (even a =delete one) for either all or none of the copy/move/destructor functions.
Is that too strong?
- Yes, this will catch some errors, as noted: "to avoid unwanted effects like turning all potential moves into more expensive copies, or making a class move-only." That's good.
- But this will also require programmers to write redundant boilerplate for classes that don't need it, such as classes that have user-written copy operations where move cannot be more efficient than copy and the programmer knows that writing only copy will cause move to be treated as copy for their type and know that's okay. Programmers hate being told to make code changes that they know don't change meaning just to satisfy a tool, and it's the kind of thing that causes them to disable a rule as noisy / low-value.
Note: The current rule and Enforcement would flag every pre-C++11 class with a user-defined copy operation.
One potential suggestion
The main noise seems to come from requiring explicit move operations. One change that could address #2164 and pre-C++11 types and most of the required-boilerplate cases would be to not require writing out move operations. Something like this:
C.21: If you define or =delete any copy, move, or destructor function, define or =delete all copy and destructor operations
[...]
Enforcement
(Simple) A class should have a declaration (even a =delete one) for all copy/destructor functions if any copy/move/destructor function is user-declared.
We could also add a note along the lines of "and if you write a copy operation check whether you should also write a more-efficient move operation, to avoid turning all potential moves into more expensive copies" -- as a Note here, or as a separate guideline.
See also #2164 where this issue is creating false positives in Guidelines checkers such as clang-tidy.
Problem
C.21 mandates the Rule of Five:
[...]
Is that too strong?
Note: The current rule and Enforcement would flag every pre-C++11 class with a user-defined copy operation.
One potential suggestion
The main noise seems to come from requiring explicit move operations. One change that could address #2164 and pre-C++11 types and most of the required-boilerplate cases would be to not require writing out move operations. Something like this:
[...]
We could also add a note along the lines of "and if you write a copy operation check whether you should also write a more-efficient move operation, to avoid turning all potential moves into more expensive copies" -- as a Note here, or as a separate guideline.